如何在Capybara中为集成测试设置请求对象?

时间:2013-03-14 21:57:07

标签: ruby-on-rails rspec capybara omniauth

request在水豚中不可用,但我试图通过facebook / twitter测试登录。如何创建帮助程序才能使用request

错误: 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

感谢您的帮助!

1 个答案:

答案 0 :(得分:2)

我是使用google_auth做到的,但您也可以使用facebook,twitter和github进行此操作。

首先删除此行:

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

在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'
       }
})

然后在你的助手中:

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

将您的助手包含在spec_helper.rb

RSpec.configure do |config|

    config.include FeatureHelpers, type: :feature

end

最后,在你的feature / capybara_spec.rb文件中

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

我知道有点晚了,也许你找到了答案,但如果你不能或任何人都在阅读,那么拥有它是好的!