Q:

RSPEC test of #update cannot update some parameters

I try testing my #UPDATE method in my Users Controller. The update of my fields (firstname, lastname, email, phone_number) works totally fine on my localhost but I can't make my test pass. Here is my code 
I have this in my Users Controller : 
def update
@user = User.find(params[:id])
authorize(@user)
@user.skip_reconfirmation! if @user.is_a_customer?
respond_to do |format|
  if @user.update(user_params)
    format.html { redirect_to user_account_path, notice: 
      I18n.t('User_was_successfully_updated') }
             format.json { render json: @user, status: :ok}
  else
    format.html { render :edit }
    format.json { render json: @user.errors, status: 
                 :unprocessable_entity }
  end
end

and here is my RSPEC test : 
 RSpec.describe UsersController, type: :controller do
  describe "PATCH update" do
    let(:user)        { create(:customer_user) }

    before :each do
      sign_in user
    end

    it "update the user information" do
      allow(User).to receive(:find_by).and_return(user)

      new_attributes = {
          firstname: 'Roger',
          lastname: 'Rabbit',
          email: 'roger.rabbit@lapin.fr',
          mobile_phone_number: '0606060606',
          gender: 'female',
      }
      patch :update, params: { locale: I18n.locale,
                                      id: user.id,
                                      user: new_attributes
                                    }
      expect(user.firstname).to eq(new_attributes['firstname'])
      expect(user.lastname).to eq(new_attributes['lastname'])
      expect(user.email).to eq(new_attributes[:email])
      expect(user.mobile_phone_number).to 
         eq(new_attributes[:mobile_phone_number])
    end

Finally, here is the error message : 
  0) UsersController PATCH update user is a user update the user information
 Failure/Error: expect(user.email).to eq(new_attributes[:email])

   expected: "roger.rabbit@lapin.fr"
        got: "customer@ecity.fr"

   (compared using ==)
 # ./spec/controllers/users_controller_spec.rb:29:in `block (4 levels) in <top (required)>'

Two more things : 
- the test pass for firstname and lastname
- new_attributes['email'] and new_attributes['mobile_phone_number'] return nils ; that's why I symbolized key for the two last expectations

A:

You need to reload the user object.
patch :update, ...

user.reload # ⇐ THIS

expect(user.firstname).to ...

Without reloading you have the stale object in your test and Rails has no ability to magically understand the object in the database has changed.

