Closed idrissd closed 7 years ago
I think it has to be with the user model, try dropping your database and migrate again
First I want to thank you @kurenn on a great tutorial! :) I'm also on Rails 5 and I too am having this issue right now. Do you know what this might be? When I print to the log I can see that user_password and user_email in the sessions controller are nil. I have no clue as to why this happens. :/
def create
user_password = params[:session][:password]
user_email = params[:session][:email]
user = user_email.present? && User.find_by(email: user_email)
if user.valid_password? user_password
sign_in user, store: false
user.generate_authentication_token!
user.save
render json: user, status: 200, location: [:api, user]
else
render json: { errors: "Invalid email or password" }, status: 422
end
end
This happens on your tests or with a client like postman or curl?
This happens with my tests. You'll have to excuse me, this is all fairly new to me and I have don't really know how to do a post request yet in postman which would obviously help to see if the tests ar at fault or if something's changed with Rails 5 for some reason. I am pretty confident it has nothing to do with the code itself. I have double and triple checked, going so far as to copy you code to see if I have missed something. These are the failing ones from session_controller_spec
"when the credentials are correct" do
before(:each) do
credentials = { email: @user.email, password: "12345678" }
post :create, { session: credentials }
end
it "returns the user record corresponding to the given credentials" do
@user.reload
expect(json_response[:auth_token]).to eql @user.auth_token
end
it { should respond_with 200 }
end``
Failures:
1) Api::V1::SessionsController POST #create when the credentials are correct should respond with 200 Failure/Error: it { should respond_with 200 } Expected response to be a 200, but was 422
2) Api::V1::SessionsController POST #create when the credentials are correct returns the user record corresponding to the given credentials Failure/Error: expect(json_response[:auth_token]).to eql @user.auth_token
expected: "bq7t5CFguqJEX2Cxb2rL"
got: nil
(compared using eql?)
# ./spec/controllers/api/v1/sessions_controller_spec.rb:20:in `block (4 levels) in <top (required)>'
I got around to fixing the issue. If anyone else has the same issue, the problem is with the tests themselves in the sessions_controller_spec.
Change the line:
post :create, { session: credentials }
to:
post :create, params: { session: credentials }, format: :json
Thanks @saturnia I'll close this then!
@kurenn I am using Rails 5 with your book and I am getting these failures. would you help me please?
Failures:
here is my
SessionsController_spec