我们的代码:

 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相关问答推荐

Rails 7.1解决冲突的Zeitwerk Inflection规则

Rails 7.1.3如何在生产环境中查看堆栈跟踪

`heroku Open`和`heroku run rake db:Migrate`不工作(";没有这样的文件或目录";)

确定当前太平洋时区是PST还是PDT

ActiveRecord 包括.指定包含的列

Gem 更新:在从 ASCII-8BIT 到 UTF-8 到 US-ASCII 的转换中,无法将 "\xE7" 转换为 UTF-8

jQuery ajax 请求不会触发 Rails 控制器的 JS 响应?

价格字段的字符串、小数或浮点数据类型?

可能在rails中有多态has_one关系?

activerecord 查找所有未包含在数组中的内容

使用 oauth 和 twitter ruby​​ gem 时不断收到 OAuth::Unauthorized 错误

Rails Associations - has_many => :through - 但相同的模型

为什么 Ruby on Rails 使用 http://0.0.0.0:3000 而不是 http://localhost:3000?

如何打印出范围之间的随机数?

禁用 JS/Ajax 请求时的 ActionController::InvalidAuthenticityToken

何时使用 Helpers 而不是 Partials

在rails国际化yml文件中传递变量

在任何来源中都找不到 thread_safe-0.3.0

将新管理员添加到活动管理员

如何在 Ruby 类/模块命名空间中翻译模型?