3

I'm looking for a simple example for a omniauth-facebook, devise, capybara, capybara-mechanize, (maybe with VCR), and rpsec integration test...

Capybara can't connect to to an external website. So capybara-mechanize would be needed. Maybe VCR could speed things up. This link describes how to get test facebook users: https://developers.facebook.com/docs/test_users/

Using a mock, as described in the omniauth docs works well for testing the logic of the model and controller. This requires the following code in specs/helpers/omniauth.rb and calling set_omniauth in the :before of your rspec integration test.

However, it would be nice to have a true integration test that runs against facebook servers. Surely, you'd want to flag such slow tests so they do not run via guard and the default rake task.

# You can read about this gist at: http://wealsodocookies.com/posts/how-to-test-facebook-login-using-devise-omniauth-rspec-and-capybara
# which is for twitter. Below is for facebook

def set_omniauth(opts = {})
  default = {:provider => :facebook,
             :uuid     => "1234",
             :facebook => {
                 :email => "foobar@example.com",
                 :gender => "Male",
                 :first_name => "foo",
                 :last_name => "bar",
                 :nickname => "foo bar",
                 :image => 'http://graph.facebook.com/659307629/picture?type=square'
             }
  }

  credentials = default.merge(opts)
  provider = credentials[:provider]
  user_hash = credentials[provider]

  OmniAuth.config.test_mode = true

  h = {
    'provider' => provider,
    'uid' => credentials[:uuid],
    'info' => user_hash,
    "extra" => {
        "info" => user_hash
    }
  }

  OmniAuth.config.mock_auth[provider] = CollectionUtility.deep_stringify_keys(h)


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
Myron Marston
  • 21,452
  • 5
  • 64
  • 63
justingordon
  • 12,553
  • 12
  • 72
  • 116

0 Answers0