【问题标题】:How do you setup a request object for integration tests in Capybara?如何在 Capybara 中为集成测试设置请求对象?
【发布时间】:2014-06-19 05:15:23
【问题描述】:

request 在 capybara 中不可用,但我正在尝试通过 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

感谢您的帮助!

【问题讨论】:

  • 它不是专门针对 OmniAuth,但此 SO Q&A 可能有助于将 request 对象纳入您的集成规范:stackoverflow.com/q/3768718/567863
  • @PaulFioravanti 的建议对我不起作用。您是否尝试过使用page.driver.request.env 而不仅仅是request.env

标签: ruby-on-rails rspec capybara omniauth


【解决方案1】:

我使用 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

我知道有点晚了,也许你找到了答案,但如果你不能或有人正在阅读,那就太好了!

【讨论】:

  • 这对我有帮助。谢谢你写它。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-09-04
  • 2012-12-09
  • 2012-07-10
  • 1970-01-01
  • 2017-01-26
相关资源
最近更新 更多