title: How do I write an RSpec test that confirms Password Reset Token has been Saved to the DB?
body: <p>I'm writing a test for my Rails 5 API to confirm a password_reset_token is generated whenever a user requests a password reset. </p>
  
  <p>I have a short password_reset_controller that handles the initial request: </p>
  
  <pre><code>class PasswordResetController &lt; ApplicationController
    def new
      @user = User.find_by(email: params[:email].to_s.downcase)
      @user.send_password_reset if @user
    end
  end
  </code></pre>
  
  <p>send_password_reset is a method in my user model which calls the the generate_password_reset_instructions: </p>
  
  <pre><code>def send_password_reset
    self.generate_password_reset_instructions
    UserMailer.password_reset(self).deliver
  end
  
  def generate_password_reset_instructions
      self.password_reset_token = SecureRandom.hex(10)
      self.password_reset_sent_at = Time.now.utc
      save
  end
  </code></pre>
  
  <p>This should save the password_reset_token to my db (indeed, when I debug it, I know it goes through these lines of code). The following test suite passes with the exception of the last one: </p>
  
  <pre><code>require 'rails_helper'
  
  RSpec.describe 'Password Reset API' do
      let!(:user) { create(:user) }
    describe 'POST password_reset/new' do
  
      it 'returns status code 204 with user found' do
        post '/password_reset/new', params: {email: user.email}
        expect(response).to have_http_status(204)
      end
  
      it 'returns status code 204 with no user found' do
        post '/password_reset/new', params: {email: 'not#anemail.com'}
        expect(response).to have_http_status(204)
      end
  
      it "sends an email" do
        expect { UserMailer.password_reset(user).deliver }.to change { ActionMailer::Base.deliveries.count }.by(1)
      end
  
      it 'confirmation_email is sent to the right user' do
        UserMailer.password_reset(user).deliver
        mail = ActionMailer::Base.deliveries.last
        expect(mail.to[0]).to eq user.email
      end
  
      it 'generates password_reset_token' do
        expect { post '/password_reset/new', params: {email: user.email} }.to change { user.password_reset_token }
      end
    end
  end
  </code></pre>
  
  <p>The result I get in the terminal is: </p>
  
  <pre><code>Failures:
  
    1) Password Reset API POST password_reset/new generates password_reset_token
       Failure/Error: expect { post '/password_reset/new', params: {email: user.email} }.to change { user.password_reset_token }
         expected result to have changed, but is still nil
       # ./spec/requests/password_reset_spec.rb:28:in `block (3 levels) in &lt;top (required)&gt;'
       # ./spec/rails_helper.rb:56:in `block (3 levels) in &lt;top (required)&gt;'
       # ./spec/rails_helper.rb:55:in `block (2 levels) in &lt;top (required)&gt;'
  </code></pre>
  
  <p>This seems like it might be caused by the value not saving to the test db, but following suggestions in other posts (Like moving <code>ENV['RAILS_ENV'] ||= 'test'</code>) to the top of rails_helper.rb does not solve the problem. I know that the actual methods work, as I'm able to get the desired result manually by running the server and making requests, I just can't write a test to confirm that it is indeed working. As the syntax in my test would indicate, I'm using FactoryGirl in my tests to generate users. </p>
  
  <p>Any ideas as to why the last test (<code>it 'generates password_reset_token' do</code>) does not pass? </p>