I have a users_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 receive the next params
(rdb:1) pp params
{"user"=>"1", "controller"=>"users", "action"=>"create"}
Why “user” => “1” ?, why is not passing the user object properly ?
post :createexpects attributes for the user model that it will use to create a user record. you are seeing “user” => “1” because it is passing in the id of the user you created into the :user parameter.You dont want to create a user record to test the create action. You want to create a hash of attributes for the create action to create the record.
You could write something like this (assuming this would pass your model validations):