どのexampleでもロードさせたいFixtureについては、spec_helperに定義すると便利です。
spec_helper.rb
Spec::Runner.configure do |config|
…
config.global_fixtures = :table_name
…
end
ところが、テーブル名は"table_names"で、モデル名は"ModelName"というように、ActiveRecordの規約に従っていない場合、以下のようなエラーが出てロードできません。
Unable to load table_name, underlying cause no such file to load -- table_name
もちろん、ExampleGroupの中でFixturesを宣言する場合は、以下のようにset_fixture_classを定義すれば、テーブル名とモデル名をマッピングしてロードしてくれます。
hoge_spec.rb
describe 'hoge' do
set_fixture_class :table_names => ModelName
fixtures :table_names
…
end
グローバルなfixturesではどうしたらいいのでしょうか?それらしきメソッドは探してもありませんでした。
そこで、rspec-railsのソースをみてみると、global_fixtures=などのメソッドは、ActiveSupport::TestCaseのクラスメソッドを呼んでいるだけでした。試しに、以下のように書くと、グローバルfixtureもロードされるようになりました。
spec_helper.rb
Spec::Runner.configure do |config|
…
ActiveSupport::TestCase.set_fixture_class :table_names => ModelName
config.global_fixtures = :table_names
…
end