1

request is not available in capybara but I am trying to test login via facebook/twitter. How do I create a helper to be able to use request?

Error: NameError: undefined local variable or method 'request'

login_integration_tests.rb:

  before do
    OmniAuth.config.mock_auth[:facebook] = {
      'provider' => 'facebook',
      'uid' => '123545'
    }
    request.env["omniauth.auth"] = OmniAuth.config.mock_auth[:facebook] # error here
  end

Thanks for your help!

carbonr
  • 6,049
  • 5
  • 46
  • 73
  • It's not OmniAuth specifically, but this SO Q&A may be of assistance in getting the `request` object into your integration spec: http://stackoverflow.com/q/3768718/567863 – Paul Fioravanti Mar 14 '13 at 23:56
  • What @PaulFioravanti suggested did not work for me. Did you try using `page.driver.request.env` instead of just `request.env`? – Gaston Feb 02 '14 at 14:58

1 Answers1

2

I did this using google_auth, but you can do the same with facebook, twitter and github.

First of all remove this line:

request.env["omniauth.auth"] = OmniAuth.config.mock_auth[:facebook]

Add those lines, in your spec_helper.rb:

Capybara.default_host = 'http://localhost:3000' #This is very important!

OmniAuth.config.test_mode = true
OmniAuth.config.add_mock(:default, {
  :info => {
          :email => 'foobar@test.com',
          :name => 'foo',
          :password => 'qwerty123'
       }
})

Then in your helper:

module FeatureHelpers
  def login_with_oauth(service = :google_apps)
    visit "/users/auth/#{service}"
  end
end

Include your helper into spec_helper.rb

RSpec.configure do |config|

    config.include FeatureHelpers, type: :feature

end

And finally, in your feature/capybara_spec.rb file

feature "Signing in" do

  before do
    Capybara.current_driver = :selenium
    login_with_oauth #call your helper method
  end

  scenario "Signing in with correct credentials" do
    expect(page).to have_content('Welcome')
  end
end

I know is kinda late and maybe you found the answer, but if you couldn't or anybody is reading this is good to have it!

sdasdsad
  • 21
  • 4