Controller spec未知关键字:id

时间:2017-05-05 05:26:45

标签: ruby-on-rails ruby rspec ruby-on-rails-5 rspec-rails

我有简单的动作节目

def show
  @field = Field.find_by(params[:id])
end

我希望为它编写规范

require 'spec_helper'

RSpec.describe FieldsController, type: :controller do

    let(:field) { create(:field) }

  it 'should show field' do
    get :show, id: field
    expect(response.status).to eq(200)
  end
end

但我收到了错误

Failure/Error: get :show, id: field

 ArgumentError:
   unknown keyword: id

如何解决?

1 个答案:

答案 0 :(得分:140)

  

HTTP请求方法只接受以下关键字参数   params, headers, env, xhr, format

根据新API,在这种情况下,您应该使用关键字参数params

  it 'should show field' do
    get :show, params: { id: field.id }
    expect(response.status).to eq(200)
  end