Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/ruby-on-rails/58.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ruby on rails 如何在Capybara中为集成测试设置请求对象?_Ruby On Rails_Rspec_Capybara_Omniauth - Fatal编程技术网

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

Ruby on rails 如何在Capybara中为集成测试设置请求对象?,ruby-on-rails,rspec,capybara,omniauth,Ruby On Rails,Rspec,Capybara,Omniauth,请求在capybara中不可用,但我正在尝试通过facebook/twitter测试登录。如何创建一个能够使用请求的帮助程序 错误: name错误:未定义的局部变量或方法“请求” 登录\集成\测试.rb: before do OmniAuth.config.mock_auth[:facebook] = { 'provider' => 'facebook', 'uid' => '123545' } request.env["omnia

请求
在capybara中不可用,但我正在尝试通过facebook/twitter测试登录。如何创建一个能够使用
请求的帮助程序

错误:
name错误:未定义的局部变量或方法“请求”

登录\集成\测试.rb:

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

谢谢你的帮助

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

我知道有点晚了,也许你找到了答案,但是如果你找不到或者有人在读这篇文章,那就很好了

它不是OmniAuth,但这可能有助于将
请求
对象纳入您的集成规范:@PaulFioravanti建议的对我不起作用。您是否尝试过使用
page.driver.request.env
而不仅仅是
request.env
?这对我很有帮助。谢谢你写这封信。