title: How do I mock an OmniAuth hash using Rails and minitest?
body: <p>I'm using Rails 5 with minitest.  I want to mock logging into my sessions controller, which relies on omniauth (I use Google and FB for login).  I have this in my controller test, test/controllers/rates_controller_test.rb, </p>
  
  <pre><code> class RatesControllerTest &lt; ActionDispatch::IntegrationTest
  
    # Login the user
    def setup
      logged_in_user = users(:one)
      login_with_user(logged_in_user)
    end
  </code></pre>
  
  <p>and then I try and set up login in my test helper, test/test_helper.rb, </p>
  
  <pre><code>class ActiveSupport::TestCase
    # Setup all fixtures in test/fixtures/*.yml for all tests in alphabetical order.
    fixtures :all
  
    def setup_omniauth_mock(user)
      OmniAuth.config.test_mode = true
      omniauth_hash = { 'provider' =&gt; 'google',
                        'uid' =&gt; '12345',
                        'info' =&gt; {
                           'name' =&gt; "#{user.first_name} #{user.last_name}",
                            'email' =&gt; user.email,
                        },
                        'extra' =&gt; {'raw_info' =&gt;
                                        { 'location' =&gt; 'San Francisco',
                                          'gravatar_id' =&gt; '123456789'
                                        }
                        }
      }
  
      OmniAuth.config.add_mock(:google, omniauth_hash)
    end
  
    # Add more helper methods to be used by all tests here...
    def login_with_user(user)
      setup_omniauth_mock(user)
      post sessions_path
    end
  </code></pre>
  
  <p>However, when I run my controller test, I'm getting a nil value when this line is evaluated in my sessions controller ...</p>
  
  <pre><code>user = User.from_omniauth(env["omniauth.auth"])
  </code></pre>
  
  <p>Above, 'env["omniauth.auth"]' is evaluating to nil.  </p>