Search code examples
ruby-on-railsrubytestingrspecrspec-rails

ArgumentError when trying to pass nested parameters in rspec


When I'm trying to execute the following line

post :create, user: Fabricate.attributes_for(:user)

I get this error message ArgumentError: unknown keyword: user.
Here's the code I was originally trying to run:

describe 'POST create' do
   context 'with valid input' do
     it 'creates the user' do
       post :create, user: Fabricate.attributes_for(:user)
       expect(User.count).to eq(1)
     end
   end
end

Solution

  • One of the changes in Rails 5 is that the testing request methods accept only keyword arguments instead of passing any arbitrary hash option as a param which was the behavior in Rails 4.

    post :create, params: { user: Fabricate.attributes_for(:user) }