Created
September 7, 2011 11:48
-
-
Save nicalpi/1200365 to your computer and use it in GitHub Desktop.
Testing Omniauth with Devise, Rspec and Capybara
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
background do | |
set_omniauth() | |
click_link_or_button 'Sign up with Facebook' | |
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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 => "[email protected]", | |
: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 |
Thanks, helpful.
have the problem with testing devise
class ArticlesControllerTest < ActionController::TestCase
setup do
default = {:provider => :facebook,
:uuid => "1234",
:facebook => {
:email => "[email protected]",
: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?
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.
OmniAuth.config.mock_auth[provider] = {
'uid' => credentials[:uuid],
"extra" => {
"user_hash" => {
"email" => user_hash[:email],
etc. etc. etc.
After supplying the above, it does not seem to be saving all the pertinent details in the request.env["omniauth.auth"]
hash. Am having problems with the integration test. advice much appreciated
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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!