2012-04-01 24 views
3

我想在我的RSpec测试中访问灯具数据,但似乎没有加载任何。RSpec 2似乎没有加载任何灯具?

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> 

任何想法?

回答

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 

再来看看测试数据库。装置是否被装载?

+0

文件存在,配置完全一样。这是'puts FundType.all' - >'[]'的输出,所以没有记录。 – Cojones 2012-04-01 11:11:25

+0

你可以尝试删除ERB?所以像这样...术语:“2012-04-01”(也可以查看奇怪的间距或制表符,或者尾随空格,或没有读取权限的文件) – joelparkerhenderson 2012-04-01 11:16:32

+0

不可以,不会帮助... – Cojones 2012-04-01 11:30:11

相关问题