I'm trying to access fixture data in my RSpec tests but doesn't seem to load any.
spec_helper.rb
config.fixture_path = "#{::Rails.root}/db/fixtures"
config.use_transactional_fixtures = true
config.global_fixtures = :all
This is one of my fixtures:
cash:
name: cash
interest: 1
term: <%= Time.now.to_s :db %>
Now a test is creating up a user which has a after_create callback:
ft = FundType.find_by_name("cash")
Fund.create(fund_type_id: ft.id, amount: 1000) <--- this is where the error occurs
Error:
Called id for nil, which would mistakenly be 4 -- if you really wanted the id of nil, use object_id>
Any ideas?
Try this:
ft = FundType.find_by_name("cash")
puts ft
puts ft.id
Verify your fixtures are in the directory:
ls -1 db/fixtures
Verify your configuration:
RSpec.configure do |config|
config.mock_with :rspec
config.fixture_path = "#{::Rails.root}/db/fixtures"
config.use_transactional_fixtures = true
config.global_fixtures = :all
end
Credit to the page that was previously here: http://www.etagwerker.com/2011/08/how-to-load-all-fixtures-testing-with-rspec-2-and-rails-3/
Verify your test database comes up correctly:
rake db:test:prepare
Then look at the test database. Did the fixtures get loaded?
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With