RSpec passes object as parameter to controller_spec

I have user_controller_spec.rb with this:

describe "POST create" do
  describe "with valid params" do
    let(:user) { create(:user) }

    it "assigns a newly created user as @user" do
      post :create, user: user
      assigns(:user).should be_a(User)
      assigns(:user).should be_persisted
    end
  end

 ...

end

      

Debuggin I found that the controller gets the following parameters

(rdb:1) pp params
{"user"=>"1", "controller"=>"users", "action"=>"create"}

      

Why is "user" => "1" ?, why isn't the custom object traversing properly?

+3


source to share


1 answer


post :create

expects the attributes for the custom model to be used to create the custom post. you see "user" => "1" because it passes the user id you created into a custom parameter.

You don't want to create a custom entry to validate the create action. You want to create a hash of attributes for the create action to create a record.



You could write something like this (assuming it passes your model's validation):

user_attributes = { :email => "something@example.com", :username => "something" }
post :create, user: user_attributes

      

+3


source







All Articles