kurenn / market_place_api

The API on Rails tutorial application
142 stars 68 forks source link

Rspec test failing in Rails 5 #81

Closed idrissd closed 7 years ago

idrissd commented 7 years ago

@kurenn I am using Rails 5 with your book and I am getting these failures. would you help me please?

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
     # ./spec/controllers/api/v1/sessions_controller_spec.rb:23:in `block (4 levels) in <top (required)>'

  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: ""
            got: nil

       (compared using eql?)
     # ./spec/controllers/api/v1/sessions_controller_spec.rb:20:in `block (4 levels) in <top (required)>'

Finished in 0.12454 seconds (files took 1.54 seconds to load)
5 examples, 2 failures

Failed examples:

rspec ./spec/controllers/api/v1/sessions_controller_spec.rb:23 # Api::V1::SessionsController POST #create when the credentials are correct should respond with 200
rspec ./spec/controllers/api/v1/sessions_controller_spec.rb:18 # Api::V1::SessionsController POST #create when the credentials are correct returns the user record corresponding to the given credentials

here is my SessionsController_spec

require 'spec_helper'

# RSpec.describe SessionsController, type: :controller do
describe Api::V1::SessionsController  do
  describe "POST #create" do

   before(:each) do
    @user = FactoryGirl.create :user
   end

    context "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

    context "when the credentials are incorrect" do
      before(:each) do
        credentials = { email: @user.email, password: "invalidpassword" }
        post :create, { session: credentials }
      end

      it "returns a json with an error" do
        expect(json_response[:errors]).to eql "Invalid email or password"
      end

      it { should respond_with 422 }
    end
  end

  describe "DELETE #destroy" do
    before(:each) do
      @user = FactoryGirl.create :user
      sign_in @user
      delete :destroy, id: @user.auth_token
    end

    it { should respond_with 204 }

  end
end
kurenn commented 7 years ago

I think it has to be with the user model, try dropping your database and migrate again

saturnia commented 7 years ago

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
kurenn commented 7 years ago

This happens on your tests or with a client like postman or curl?

saturnia commented 7 years ago

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

./spec/controllers/api/v1/sessions_controller_spec.rb:23:in `block (4 levels) in <top (required)>'

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)>'
saturnia commented 7 years ago

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

kurenn commented 7 years ago

Thanks @saturnia I'll close this then!