如何使用 rspec 测试传递到 Rails 3 中的控制器的参数?

发布于 2024-12-15 22:10:05 字数 986 浏览 0 评论 0 原文

我们的代码:

 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?

Our code:

 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

I got the following error message:

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

吃不饱 2024-12-22 22:10:05
get :show, {:id => subcategory.id.to_s, :params => {:sort => 'title'}}

应该是

get :show, :id => subcategory.id.to_s, :sort => 'title'

除非你想传递params[:params][:sort]

helper.params[:sort].should_not be_nil

应该是

controller.params[:sort].should_not be_nil
controller.params[:sort].should eql 'title'

(如果你想测试一个助手,你应该编写一个助手规范。)

get :show, {:id => subcategory.id.to_s, :params => {:sort => 'title'}}

Should be

get :show, :id => subcategory.id.to_s, :sort => 'title'

Unless you mean to pass params[:params][:sort].

Also

helper.params[:sort].should_not be_nil

Should be

controller.params[:sort].should_not be_nil
controller.params[:sort].should eql 'title'

(If you mean to test a helper, you should write a helper spec.)

め七分饶幸 2024-12-22 22:10:05

在 Rails 5 中,参数 API 发生了变化:

get :show, params: { id: subcategory.id.to_s, sort: 'title' }

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:

get :show, params: { id: subcategory.id.to_s, sort: 'title' }

https://relishapp.com/rspec/rspec-rails/v/3-7/docs/request-specs/request-spec#specify-managing-a-widget-with-rails-integration-methods

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文