Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.0k views
in Technique[技术] by (71.8m points)

ruby on rails - Controller spec unknown keyword: id

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?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...