【发布时间】:2015-08-01 19:55:34
【问题描述】:
我正在尝试为渲染模板运行测试并遇到错误:
undefined method `key?' for 1014:Fixnum
我的模型实例的存根在我的路由测试中可以正常工作,但在这里没有那么多。我做错了什么?
describe RestaurantsController do
let(:restaurant) { FactoryGirl.build_stubbed(:restaurant) }
describe 'GET #show' do
before { get :show, restaurant.id }
it { should render_template('show') }
end
end
完全错误
1) RestaurantsController GET #show
Failure/Error: before { get :show, restaurant.id }
NoMethodError:
undefined method `key?' for 1014:Fixnum
# /Library/Ruby/Gems/2.0.0/gems/actionpack-4.2.0/lib/action_controller/test_case.rb:744:in `html_format?'
# /Library/Ruby/Gems/2.0.0/gems/actionpack-4.2.0/lib/action_controller/test_case.rb:598:in `process'
# /Library/Ruby/Gems/2.0.0/gems/actionpack-4.2.0/lib/action_controller/test_case.rb:65:in `process'
# /Library/Ruby/Gems/2.0.0/gems/devise-3.5.1/lib/devise/test_helpers.rb:19:in `block in process'
# /Library/Ruby/Gems/2.0.0/gems/devise-3.5.1/lib/devise/test_helpers.rb:72:in `catch'
# /Library/Ruby/Gems/2.0.0/gems/devise-3.5.1/lib/devise/test_helpers.rb:72:in `_catch_warden'
# /Library/Ruby/Gems/2.0.0/gems/devise-3.5.1/lib/devise/test_helpers.rb:19:in `process'
# /Library/Ruby/Gems/2.0.0/gems/actionpack-4.2.0/lib/action_controller/test_case.rb:505:in `get'
# ./spec/controllers/restaurants_controller_spec.rb:15:in `block (3 levels) in <top (required)>'
【问题讨论】:
-
get :show, id: restaurant -
这样我得到:
Failure/Error: before { get :show, id: restaurant } ActiveRecord::RecordNotFound: Couldn't find Restaurant -
Couldn't find Restaurantraise 因为你build_*,餐厅而不是create_*。 -
@Зелёный 如果我要验证我的 Restaurant 模型的唯一性,我不会遇到重复错误吗?
-
如果您不将记录保存到数据库,则无法通过 show 操作获取它。为避免唯一性错误,请使用
database-cleaner
标签: ruby-on-rails testing rspec tdd shoulda