Controller Spec Unknown Keyword: Id

Controller spec unknown keyword: id

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

Failure/Error: get :show, id: @user.id, format: :json

You should be passing a params hash containing the parameters you're interested in submitting:

get :show, params: { id: @user.id }

rspec post:create action ArgumentError: unknown keyword: post

The code have to be like this

 describe 'POST create' do
it 'creates a new Post' do
expect {
post :create, params: {topic_id: @topic, post: @post_attributes}
}.to change(Post, :count).by(1)
end

And this works now

Rspec post :create 'unknown keyword: '

The way you're calling post does not work anymore in Rails 5.

This is the deprecation warning of the previous version:

ActionController::TestCase HTTP request methods will accept only keyword arguments in future Rails versions.

Now you have to add the params key, like this:

post :create, params: { protocol: params }

Notice that post :create, :params => { :protocol => params } means the same, it's just a different syntax.

adding headers to a rspec get

Unfortunately, rspec doesn't allow to set request headers, so you'll need to workaround it like this:

  it "returns albums" do
request.headers.merge!(authenticated_header("admin", "123"))
get "/index", params: {}
puts response.body
end


Related Topics



Leave a reply



Submit