Controller spec unknown keyword: id
Asked Answered
M

1

71

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?

Moriahmoriarty answered 5/5, 2017 at 5:26 Comment(1)
That's Rails 4 syntax. In Rails 5 and above, you need to specify the "params" keyword.Ageless
P
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
Polito answered 5/5, 2017 at 5:32 Comment(3)
I had this error start appearing after upgrading from Rails 4.2 to Rails 5.1 and this fixed it - thanks!Scripture
Life Saver :) But where is this new API defined? I found this mention but it would be good to see where/when/why this change happened (if you know!)Plenary
but in rails 6 im getting no routes matchesGraft

© 2022 - 2024 — McMap. All rights reserved.