2012-04-01 75 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

相關問題