Create a gist now

Instantly share code, notes, and snippets.

What would you like to do?
Testing Omniauth with Devise, Rspec and Capybara
background do
set_omniauth()
click_link_or_button 'Sign up with Facebook'
end
# You can read about this gist at: http://wealsodocookies.com/posts/how-to-test-facebook-login-using-devise-omniauth-rspec-and-capybara
def set_omniauth(opts = {})
default = {:provider => :facebook,
:uuid => "1234",
:facebook => {
:email => "foobar@example.com",
:gender => "Male",
:first_name => "foo",
:last_name => "bar"
}
}
credentials = default.merge(opts)
provider = credentials[:provider]
user_hash = credentials[provider]
OmniAuth.config.test_mode = true
OmniAuth.config.mock_auth[provider] = {
'uid' => credentials[:uuid],
"extra" => {
"user_hash" => {
"email" => user_hash[:email],
"first_name" => user_hash[:first_name],
"last_name" => user_hash[:last_name],
"gender" => user_hash[:gender]
}
}
}
end
def set_invalid_omniauth(opts = {})
credentials = { :provider => :facebook,
:invalid => :invalid_crendentials
}.merge(opts)
OmniAuth.config.test_mode = true
OmniAuth.config.mock_auth[credentials[:provider]] = credentials[:invalid]
end

cew821 commented Mar 4, 2014

The link referenced above no longer works, but you can find the post at the new URL: http://cookieshq.co.uk/posts/how-to-test-facebook-login-using-devise-omniauth-rspec-and-capybara/

Thanks for posting it!

alagu commented May 5, 2014

Thanks, helpful.

have the problem with testing devise

class ArticlesControllerTest < ActionController::TestCase
  setup do
    default = {:provider => :facebook,
               :uuid     => "1234",
               :facebook => {
                 :email => "foobar@example.com",
                 :gender => "Male",
                 :first_name => "foo",
                 :last_name => "bar"
               }
    }
    opts = {}
    credentials = default.merge(opts)
    provider = credentials[:provider]
    user_hash = credentials[provider]

    OmniAuth.config.test_mode = true

    OmniAuth.config.mock_auth[provider] = {
      'uid' => credentials[:uuid],
      "extra" => {
        "user_hash" => {
          "email" => user_hash[:email],
          "first_name" => user_hash[:first_name],
          "last_name" => user_hash[:last_name],
          "gender" => user_hash[:gender]
        }
      }
    }
  end
  test "should get index" do
    get :index
    assert_response :success
  end
end

but I have an error ActionView::Template::Error: No route matches {:controller=>"users/omniauth_callbacks", :action=>"passthru", :provider=>:facebook}

What should I do?

QBD commented Jan 8, 2015

Thanks for this, I was having an issue with invalid credentials when running a test after calling OmniAuth.config.mock_auth[:facebook] = :invalid_credentials on a previous test.

This approach lets me control the credentials that are being supplied with greater accuracy.

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