gpt4 book ai didi

ruby-on-rails - 如何使用 rspec 测试传递到 Rails 3 中的 Controller 的参数?

转载 作者:行者123 更新时间:2023-12-03 05:40:32 26 4
gpt4 key购买 nike

我们的代码:

 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)>'

如何测试 rspec 中的参数?

最佳答案

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'

(如果您打算测试助手,则应该编写助手规范。)

关于ruby-on-rails - 如何使用 rspec 测试传递到 Rails 3 中的 Controller 的参数?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8160284/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com