RSpec 2似乎没有加载任何灯具?

时间:2012-04-01 10:50:23

标签: ruby-on-rails ruby-on-rails-3 rspec rspec2 fixtures

我试图在我的RSpec测试中访问夹具数据,但似乎没有加载任何夹具数据。

spec_helper.rb

config.fixture_path = "#{::Rails.root}/db/fixtures"
config.use_transactional_fixtures = true
config.global_fixtures = :all

这是我的一个装置:

cash:
  name: cash
  interest: 1
  term: <%= Time.now.to_s :db %>

现在测试正在创建一个具有after_create回调的用户:

ft = FundType.find_by_name("cash")
Fund.create(fund_type_id: ft.id, amount: 1000) <--- this is where the error occurs

错误:

Called id for nil, which would mistakenly be 4 -- if you really wanted the id of nil, use object_id>

有什么想法吗?

1 个答案:

答案 0 :(得分:5)

试试这个:

ft = FundType.find_by_name("cash")
puts ft
puts ft.id

确认你的灯具在目录中:

 ls -1 db/fixtures

验证您的配置:

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

相信之前的页面: http://www.etagwerker.com/2011/08/how-to-load-all-fixtures-testing-with-rspec-2-and-rails-3/

验证您的测试数据库是否正确显示:

 rake db:test:prepare

然后查看测试数据库。夹具是否装好了?