Controller spec unknown keyword: id

Ruby on-RailsRubyRspecRuby on-Rails-5Rspec Rails

Ruby on-Rails Problem Overview


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?

Ruby on-Rails Solutions


Solution 1 - Ruby on-Rails

> 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

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionuserView Question on Stackoverflow
Solution 1 - Ruby on-RailsPhilidorView Answer on Stackoverflow