title: How to test my update action with RSpec?
body: <p>In my Rails application I have this <strong>update</strong> action:</p>
  
  <pre><code>class UsersController &lt; 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 &lt; 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
  </code></pre>
  
  <p>Now, everything works perfectly when I test the <strong>update</strong> action manually in the browser.</p>
  
  <p>The <strong>RSpec</strong> test that I wrote, however, does not work and I can't figure out why:</p>
  
  <pre><code>it "changes @user's new_email" do
    @user = create(:user, email: "john@doe.com")
    put :update, id: @user, user: attributes_for(:user, email: "new@email.com")
    @user.reload
    expect(@user.new_email).to eq("new@email.com")
  end
  </code></pre>
  
  <p>I keep getting the same error message:</p>
  
  <blockquote>
    <p>1) UsersController user access PUT #update with valid attributes changes @user's new_email
         Failure/Error: expect(@user.new_email).to eq("new@email.com")</p>
  </blockquote>
  
  <pre><code>   expected: "new@email.com"
          got: nil
  
     (compared using ==)
  </code></pre>
  
  <p>Can anybody tell me what I am missing here?</p>
  
  <p>Thanks for any help!</p>