jeudi 24 novembre 2016

Testing password update - Rspec, Capybara, Devise

I'm pretty new to ROR and testing and i'm trying to test a password update to a RegistrationsController inherited from DeviseRegistrationsController.

I included at the end of my controller an update_resource method, showed here

My controller

class Users::RegistrationsController < Devise::RegistrationsController

 ...

 def update_resource(resource, params)
   if params[:password].blank? && params[:password_confirmation].blank?
     resource.update_without_password(params)
   else
     super
   end
 end

end

My controller test file

require "rails_helper"

RSpec.describe Users::RegistrationsController, type: :controller do

  describe "update password" do
    before do
      @request.env['devise.mapping'] = Devise.mappings[:user]
    end
    let(:user){ FactoryGirl.create(:user, password: 'current_password', password_confirmation: 'current_password') }

    context "with a valid password parameter" do
      it "updates user in the database" do

        put :update, params: { id: user, user: FactoryGirl.attributes_for(:user, password: '12', password_confirmation: '12') }
        user.reload

        expect(user.password).to eq("newpassword12")
      end
    end
  end

end

I'm receiving the follow error

 2) Users::RegistrationsController update password with a valid password parameter updates user in the database
 Failure/Error: expect(user.password).to eq("newpassword12")

   expected: "newpassword12"
        got: "current_password"

   (compared using ==)
 # ./spec/controller/users/registrations_controller_spec.rb:75:in `block (4 levels) in <top (required)>'

Any idea what am i doing wrong?

Aucun commentaire:

Enregistrer un commentaire