I'm trying to test my custom Registration Controller inherited from default Devise Registration Controller.
I created a method to allow user to updates their data without providing the current_password and updates the password providing the current password.
My custom 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 tests
require "rails_helper"
RSpec.describe Users::RegistrationsController, type: :controller do
before do
@request.env['devise.mapping'] = Devise.mappings[:user]
end
describe "POST new password" do
let(:user) { FactoryGirl.create(:user, password: 'current_password', password_confirmation: 'current_password') }
before do
login_as(user, :scope => :user)
end
context "with an invalid password parameter" do
it "renders :edit-template" do
put :update, params: { id: user.id , user: { current_password: "current_password", password: "12", password_confirmation: "12" } }
expect(response).to render_template(:edit)
end
end
context "with a valid password parameter" do
it "update user password in database" do
put :update, params: { id: user.id, user: { current_password: "current_password", password: "newpassword", password_confirmation: "newpassword" } }
user.reload
expect(user.password).to eq("newpassword")
end
end
end
end
I received the follow errors
1) Users::RegistrationsController POST new password with an invalid password parameter renders :edit-template
Failure/Error: expect(response).to render_template(:edit)
expecting <"edit"> but rendering with <[]>
# ./spec/controller/users/registrations_controller_spec.rb:59:in `block (4 levels) in <top (required)>'
2) Users::RegistrationsController POST new password with a valid password parameter update user password in database
Failure/Error: expect(user.password).to eq("newpassword")
expected: "newpassword"
got: "current_password"
(compared using ==)
# ./spec/controller/users/registrations_controller_spec.rb:68:in `block (4 levels) in <top (required)>'
Any idea what am I doing wrong?
Aucun commentaire:
Enregistrer un commentaire