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

Coj*_*nes 3 rspec ruby-on-rails fixtures rspec2 ruby-on-rails-3

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

spec_helper.rb

config.fixture_path = "#{::Rails.root}/db/fixtures"
config.use_transactional_fixtures = true
config.global_fixtures = :all
Run Code Online (Sandbox Code Playgroud)

这是我的一个装置:

cash:
  name: cash
  interest: 1
  term: <%= Time.now.to_s :db %>
Run Code Online (Sandbox Code Playgroud)

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

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

错误:

Called id for nil, which would mistakenly be 4 -- if you really wanted the id of nil, use object_id>
Run Code Online (Sandbox Code Playgroud)

有任何想法吗?

joe*_*son 5

试试这个:

ft = FundType.find_by_name("cash")
puts ft
puts ft.id
Run Code Online (Sandbox Code Playgroud)

验证您的灯具是否在目录中:

 ls -1 db/fixtures
Run Code Online (Sandbox Code Playgroud)

验证您的配置:

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
Run Code Online (Sandbox Code Playgroud)

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

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

 rake db:test:prepare
Run Code Online (Sandbox Code Playgroud)

然后看看测试数据库.夹具是否装满了?

  • 这是一个破碎的链接. (2认同)