尝试使用RSpec测试OmniAuth时,“实际HTTP连接被禁用”错误

时间:2013-12-09 03:28:04

标签: ruby-on-rails ruby rspec

我一直试图用RSpec测试OmniAuth,但它还没有用。

spec_helper.rb

OmniAuth.config.test_mode = true
OmniAuth.config.add_mock(:twitter, {:uid => '12345'})

并在spec/requests/static_pages_spec.rb

describe "for signed-in users" do
  before do
    visit "auth/twitter"
  end
  it { should have_content("Log out") }
end

我得到以下错误。

 Failure/Error: visit "auth/twitter"
 ActionView::Template::Error:
   Real HTTP connections are disabled. Unregistered request: 

根据官方文档,auth/twitter的请求应重定向到auth/twitter/callback。为什么尝试建立HTTP连接?

我已经阅读了以下网页和问题,但我无法找到测试失败的原因。

http://blog.zerosum.org/2011/03/19/easy-rails-outh-integration-testing.html https://github.com/intridea/omniauth/wiki/Integration-Testing omniauth-facebook and testing with rspec http://blog.plataformatec.com.br/2010/12/acceptance-tests-for-omniauth/

1 个答案:

答案 0 :(得分:1)

正如dfedde所暗示的那样,WebMock会阻止集成测试中的HTTP请求,但您可以存根请求或调用WebMock.allow_net_connect!来配置测试以正常运行。有关详细信息,请查看Trying to get selenium working in rails 3 - "WebMock::NetConnectNotAllowedError"https://github.com/bblimke/webmock