尝试使用RSpec测试OmniAuth时出现“禁用真实HTTP连接”错误

我一直试图用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 omn​​iauth-facebook和测试rspec http://blog.plataformatec.com.br/2010/12/acceptance-tests-for-omniauth/

正如dfedde所暗示的,WebMock在集成测试中阻止HTTP请求,但您可以存根请求或调用WebMock.allow_net_connect! 配置测试以正常运行。 查看试图让rails中的selenium工作 – “WebMock :: NetConnectNotAllowedError”或https://github.com/bblimke/webmock了解详细信息。