Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

RSpec 2 doesn't seem to load any fixtures?

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?

like image 874
Cojones Avatar asked Dec 29 '25 01:12

Cojones


1 Answers

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?

like image 77
joelparkerhenderson Avatar answered Dec 31 '25 19:12

joelparkerhenderson



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!