如何使用 rspec 测试传递到 Rails 3 中的控制器的参数?
我们的代码:
describe "GET show" do
it "assigns the requested subcategory as @subcategory" do
subcategory = Subcategory.create! valid_attributes
get :show, :id => subcategory.id.to_s
assigns(:subcategory).should eq(subcategory)
end
it "has a sort parameter in the url" do
subcategory = Subcategory.create! valid_attributes
get :show, {:id => subcategory.id.to_s, :params => {:sort => 'title'}}
helper.params[:sort].should_not be_nil
end
end
我收到以下错误消息:
1) SubcategoriesController GET show has a sort parameter in the url
Failure/Error: helper.params[:sort].should_not be_nil
NameError:
undefined local variable or method `helper' for #<RSpec::Core::ExampleGroup::Nested_4::Nested_2:0x007f81a467c848>
# ./spec/controllers/subcategories_controller_spec.rb:54:in `block (3 levels) in <top (required)>'
How can I test params in rspec?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
应该是
除非你想传递
params[:params][:sort]
。也
应该是
(如果你想测试一个助手,你应该编写一个助手规范。)
Should be
Unless you mean to pass
params[:params][:sort]
.Also
Should be
(If you mean to test a helper, you should write a helper spec.)
在 Rails 5 中,参数 API 发生了变化:
https://relishapp.com/rspec/rspec-rails/v/3-7/docs/request-specs/request-spec#specify-managing-a-widget-with-rails-integration-方法
With Rails 5 the params API changed:
https://relishapp.com/rspec/rspec-rails/v/3-7/docs/request-specs/request-spec#specify-managing-a-widget-with-rails-integration-methods