FEATURE
A user has a profile and should be able to update it.
PROBLEM
I update the profile, for example change the name to "Homer Simpson", but all the assertions fail as the database record does not seem to update.
I can't seem to get updated attributes:
Failure/Error: expect(subject.current_user.first_name).to eq('Homer')
expected: "Homer"
got: "Lew"
(compared using ==)
# ./spec/controllers/registrations_controller_spec.rb:67:in `block (3 levels) in <top (required)>'
N.B. I have tried both @user.reload
and subject.current_user.reload
Specs still not passing.
CODE
I am using:
- rails (4.0.0)
- devise (3.0.3)
- rspec-rails (2.14.0)
- capybara (2.1.0)
- factory_girl (4.2.0)
- database_cleaner (1.1.1)
I have already checked:
- I've set the
devise.mapping
to user - I don't have a
valid_session
polluting the devise session as per this other Stackoverflow thread.
registrations_controller_spec.rb
describe "User Profiles" do
login_user
it "Update - changes the user's attributes" do
put :update, id: @user, user: attributes_for(:user, first_name: 'Homer')
@user.reload
expect(@user.first_name).to eq('Homer') # FAILS
end
end
I have tried swapping @user
for subject.current_user
like in this Stackoverflow thread: "Devise Rspec registration controller test failing on update as if it was trying to confirm email address"
put :update, id: subject.current_user, user: attributes_for(:user, first_name: 'Homer')
subject.current_user.reload
expect(subject.current_user.first_name).to eq('Homer') # Still FAILS
But it still fails.
Is the problem in the controller? I find the user by current_user.id
instead of via params[:id]
.
registrations_controller.rb
def update
@user = User.find(current_user.id)
email_changed = @user.email != params[:user][:email]
password_changed = !params[:user][:password].blank?
if email_changed or password_changed
successfully_updated = @user.update_with_password(user_params)
else
successfully_updated = @user.update_without_password(user_params)
end
if successfully_updated
sign_in @user, bypass: true # Sign in the user bypassing validation in case his password changed
redirect_to user_profile_path, notice: 'Profile was successfully updated.'
else
render "edit"
end
end
controller_macros.rb - defines login_user
helper
module ControllerMacros
def login_user
before(:each) do
@request.env["devise.mapping"] = Devise.mappings[:user]
@user = FactoryGirl.create(:user)
@user.confirm!
sign_in @user
end
end
end
My integration specs pass fine. What am I missing here in the controllers?