70

I have simple action show

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

and i want write spec for it

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

but I have got an error

Failure/Error: get :show, id: field

 ArgumentError:
   unknown keyword: id

How to fix it?

Roman Kiselenko
  • 43,210
  • 9
  • 91
  • 103
user
  • 1,341
  • 2
  • 17
  • 28

1 Answers1

191

HTTP request methods will accept only the following keyword arguments params, headers, env, xhr, format

According to the new API, you should use keyword arguments, params in this case:

  it 'should show field' do
    get :show, params: { id: field.id }
    expect(response.status).to eq(200)
  end
Roman Kiselenko
  • 43,210
  • 9
  • 91
  • 103
  • 14
    I had this error start appearing after upgrading from Rails 4.2 to Rails 5.1 and this fixed it - thanks! – mahi-man May 26 '17 at 05:06
  • 7
    Life Saver :) But where is this new API defined? I found [this mention](https://relishapp.com/rspec/rspec-rails/v/3-7/docs/request-specs/request-spec#specify-managing-a-widget-with-rails-integration-methods) but it would be good to see where/when/why this change happened (if you know!) – starfry Feb 12 '18 at 13:43
  • 1
    but in rails 6 im getting no routes matches – Humayun Naseer Jan 01 '21 at 11:14