Rails 5 and Rspec Parameters

Brandon Caples
 Web Development Ruby on Rails and Rspec

Because of a deprecation to Rspec in Ruby on Rails 5, parameters must now be placed inside a paramshash.

Say, for instance, that I have a Rails 5 app in which users can create posts. The following would have worked fine in a Rails 4 controller spec:

describe "POST create" do
  it "redirects the user to the sign in view" do
    post :create, post_id: my_post.id, comment: { body: RandomData.random_paragraph }
    expect(response).to redirect_to(new_session_path)
  end
end

But Rails 5 provides the following error message:

1) CommentsController guest POST create redirects the user to the sign in view   
  Failure/Error: post :create, post_id: my_post.id, comment: { body: RandomData.random_paragraph }
  ArgumentError: unknown keywords: post_id, comment

The reason for this previously accepted syntax is deprecation. I don’t know why this decision was made, but Rails 5 requires that parameters be placed inside of a params hash. I guess it’s more human readable this way. So here are my parameters inside a params hash:

describe "POST create" do
  it "redirects the user to the sign in view" do
    post :create, params: { post_id: my_post.id, comment: { body: RandomData.random_paragraph } }
    expect(response).to redirect_to(new_session_path)
  end
end

And the errors are gone!

, ,

Author:

I'm a WordPress and Ruby web developer with marketing, SEO, and design experience.