RSpec - 未定义的方法“key?" [英] RSpec - Undefined method `key?'
问题描述
我正在尝试为渲染模板运行测试并遇到错误:
I'm trying to run a test for rendering templates and ran into the error:
undefined method `key?' for 1014:Fixnum
我的模型实例的存根在我的路由测试中正常工作,但在这里没有那么多.我做错了什么?
The stub of my model's instance works as it should in my route tests but not so much here. What am I doing wrong?
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
执行操作和参数散列(除其他外).它不会隐式地将模型转换为 { id: model.to_param }
.
get
takes a action and hash of params (among other things). It will not implicitly take a model and turn it into { id: model.to_param }
.
相反,您需要明确指定参数.
Instead you need to specify the parameters explicitly.
describe RestaurantsController do
let(:restaurant) { create(:restaurant) }
subject { response }
describe 'GET #show' do
before { get :show, id: restaurant }
it { should render_template('show') }
end
end
正如@Зелёный 已经提到的,您需要将记录实际保存到数据库中,以便能够在控制器规范中使用它.
As @Зелёный has already mentioned you need to actually persist the record to the database to be able to use it in a controller spec.
为避免重复问题和测试顺序问题,您应该在每个示例之间清空数据库.database_cleaner gem 对这项任务非常宝贵.
To avoid the duplication issue and test ordering issues you should empty the database between each example. The database_cleaner gem is invaluable for that task.
此外,如果您需要在同一个规范中创建多个记录,您可以在 factory girl 中使用序列:
Also if you need to create several records in the same spec you can use sequences in factory girl:
FactoryGirl.define do
factory :user do
email { |n| "test-#{n}@example.com" }
end
end
gem ffaker 非常适合生成电子邮件、用户名等.
The gem ffaker is great for generating emails, usernames etc.
您可以通过在 rails_helper
中包含其方法来为 FactoryGirl 设置快捷方式:
You can setup shortcuts for FactoryGirl by including its methods in your rails_helper
:
RSpec.configure do |config|
# ...
config.include FactoryGirl::Syntax::Methods
end
这使您可以使用 FactoryGirl 方法而无需像这样输入模块名称:
This lets you use the FactoryGirl methods without typing the module name like so:
let(:restaurant) { create(:restaurant) }
这篇关于RSpec - 未定义的方法“key?"的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!