In my Rails application I have this update action:
class UsersController < ApplicationController
before_filter :authorized_user
def update
current_email = @user.email
new_email = params[:user][:email].downcase.to_s
if @user.update_attributes(params[:user])
if new_email != current_email
@user.change_email(current_email, new_email)
flash[:success] = "Please click on the link that we've sent you."
else
flash[:success] = "User updated."
end
redirect_to edit_user_path(@user)
else
render :edit
end
end
private
def authorized_user
@user = User.find(params[:id])
redirect_to(root_path) unless current_user?(@user)
end
end
class User < ActiveRecord::Base
def change_email(old_email, new_email)
self.email = old_email
self.new_email = new_email.downcase
self.send_email_confirmation_link
end
end
Now, everything works perfectly when I test the update action manually in the browser.
The RSpec test that I wrote, however, does not work and I can't figure out why:
it "changes @user's new_email" do
@user = create(:user, email: "[email protected]")
put :update, id: @user, user: attributes_for(:user, email: "[email protected]")
@user.reload
expect(@user.new_email).to eq("[email protected]")
end
I keep getting the same error message:
1) UsersController user access PUT #update with valid attributes changes @user's new_email Failure/Error: expect(@user.new_email).to eq("[email protected]")
expected: "[email protected]"
got: nil
(compared using ==)
Can anybody tell me what I am missing here?
Thanks for any help!
email
,new_email
and others – Tintin81