“禁用真正的 HTTP 连接"尝试使用 RSpec 测试 OmniAuth 时出错 [英] "Real HTTP connections are disabled" error when trying test OmniAuth with RSpec
问题描述
我一直在尝试使用 RSpec 测试 OmniAuth,但它没有奏效.
I've been trying to test OmniAuth with RSpec, but yet it have not worked.
在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 连接?
According to the official document a request to auth/twitter
should be redirect to auth/twitter/callback
. Why does it try to have HTTP connection?
我已阅读以下网页和问题,但我找不到测试失败的原因.
I've read following web pages and questions, but I couldn't find why the test failed.
http://blog.zerosum.org/2011/03/19/easy-rails-outh-integration-testing.htmlhttps://github.com/intridea/omniauth/wiki/Integration-Testingomniauth-facebook 和使用 rspec 进行测试http://blog.plataformatec.com.br/2010/12/acceptance-tests-for-omniauth/
推荐答案
正如 dfedde 暗示的那样,WebMock 在集成测试中阻止 HTTP 请求,但您可以存根请求或调用 WebMock.allow_net_connect!
来将测试配置为正常运行.查看 尝试让 selenium 在 rails 3 中工作 - "WebMock::NetConnectNotAllowedError" 或 https://github.com/bblimke/webmock 了解详情.
As dfedde implies, WebMock blocks HTTP requests in integration tests, but you can either stub the request or call WebMock.allow_net_connect!
to configure the tests to run as normal. Check out Trying to get selenium working in rails 3 - "WebMock::NetConnectNotAllowedError" or https://github.com/bblimke/webmock for details.
这篇关于“禁用真正的 HTTP 连接"尝试使用 RSpec 测试 OmniAuth 时出错的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!