Skip to content

Instantly share code, notes, and snippets.

@vicmaster
Last active August 29, 2015 14:13
Show Gist options
  • Save vicmaster/61377bc3806ae4d8283f to your computer and use it in GitHub Desktop.
Save vicmaster/61377bc3806ae4d8283f to your computer and use it in GitHub Desktop.
Mocking Github Omniauth

The mock_auth configuration allows you to set per-provider (or default) authentication hashes to return during integration testing. You can set it like so:

module Omniauth

  module Mock
    def auth_mock
      OmniAuth.config.mock_auth[:github] = OmniAuth::AuthHash.new({
        provider: 'github',
        uid: '123545',
        info: {
              name: 'John',
              email: 'test@github.com',
              nickname: 'johngit',
              image: 'something.png'
              },
        credentials: {
                token: '3232322321cdd2'
              }
      })
    end
  end

  module SessionHelpers
    def signin
      visit root_path
      expect(page).to have_content("Sign in")
      auth_mock # Calling auth github mock
      click_link "Sign in"
    end
  end

end

You can set the :default key to return a hash for providers that haven't been specified. Once you set the mock auth hash and turn on test mode, all requests to OmniAuth will return an auth hash from the mock.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment