mardi 10 avril 2018

User password update rspec

In my controller, I have a method called update_password, this method updates the user password after validating token. But, my tests are failing.What might be the problem?

In my controller,

      def update_password
        user = User.find_by(email: params[:email])
        if user.nil?
          render json: { error: 'Could not update' }, status: 422
        else
          user.update(user_params)
          render json: { message: 'Successfully Updated' }, status: 200
        end
      end

     def user_params
        params.permit(:password, :password_confirmation, :current_password, :email)
      end

Test:

   describe 'update password for valid token' do
    it'should update the password' do
      user_params = {
        password: 'newpassword',
        password_confirmation: 'newpassword',
        email: user.email
      }
      put '/api/v1/update_password', params: user_params
      expect(user.password).to eq 'newpassword'
      expect(user.reload.password_confirmation).to eq 'newpassword'
      expect(user.reload.password).to eq(user.reload.password_confirmation)
      json_response = JSON.parse(response.body)
      expect(json_response['message']).to eq('Successfully Updated')
    end
   end

Factories:

FactoryBot.define do
  factory :user do
    sequence(:email) { |n| "user#{n}@example.com" }
    password 'testcase'
    username 'testcase'
    password_confirmation 'testcase'
    first_name 'testname'
    last_name 'test'
  end
end

Error I have got:

1) UsersRequests update password for valid token should update the password
     Failure/Error: expect(user.password).to eq 'newpassword'

       expected: "newpassword"
            got: "testcase"

       (compared using ==)
     # ./spec/requests/users_requests_spec.rb:105:in `block (3 levels) in <top (required)>'

Finished in 0.35031 seconds (files took 5.69 seconds to load)
1 example, 1 failure

Failed examples:

rspec ./spec/requests/users_requests_spec.rb:98 # UsersRequests update password for valid token should update the password

Aucun commentaire:

Enregistrer un commentaire