【问题标题】:Make a webmock with port range使用端口范围制作 webmock
【发布时间】:2015-09-29 15:49:23
【问题描述】:

我在describe 中使用带有js: true 的rspec 和capybara,但我有一个问题:

     Failure/Error: visit '/users/sign_in'
     WebMock::NetConnectNotAllowedError:
       Real HTTP connections are disabled. Unregistered request: GET http://127.0.0.1:62453/__identify__ with headers {'Accept'=>'*/*', 'Accept-Encoding'=>'gzip;q=1.0,deflate;q=0.6,identity;q=0.3', 'User-Agent'=>'Ruby'}

       You can stub this request with the following snippet:

       stub_request(:get, "http://127.0.0.1:62453/__identify__").
         with(:headers => {'Accept'=>'*/*', 'Accept-Encoding'=>'gzip;q=1.0,deflate;q=0.6,identity;q=0.3', 'User-Agent'=>'Ruby'}).
         to_return(:status => 200, :body => "", :headers => {})

如果我像他们说的那样把它存根,下一个端口就不同了:

  1) the signin process signs me in
     Failure/Error: visit '/users/sign_in'
     WebMock::NetConnectNotAllowedError:
       Real HTTP connections are disabled. Unregistered request: GET http://127.0.0.1:62453/__identify__ with headers {'Accept'=>'*/*', 'Accept-Encoding'=>'gzip;q=1.0,deflate;q=0.6,identity;q=0.3', 'User-Agent'=>'Ruby'}

       You can stub this request with the following snippet:

       stub_request(:get, "http://127.0.0.1:62453/__identify__").
         with(:headers => {'Accept'=>'*/*', 'Accept-Encoding'=>'gzip;q=1.0,deflate;q=0.6,identity;q=0.3', 'User-Agent'=>'Ruby'}).
         to_return(:status => 200, :body => "", :headers => {})

       registered request stubs:

       stub_request(:get, "http://127.0.0.1:61772/__identify__").
         with(:headers => {'Accept'=>'*/*', 'Accept-Encoding'=>'gzip;q=1.0,deflate;q=0.6,identity;q=0.3', 'User-Agent'=>'Ruby'})

我尝试将正则表达式添加到端口,但一直失败。

【问题讨论】:

    标签: regex rspec capybara webmock


    【解决方案1】:

    你不能同时使用 webmock 和 js: true。当设置 js: true 时,Capybara 使用单独的浏览器发出请求,因此任何拦截测试线程中的连接的尝试都将无效。
    您在输出中看到的连接尝试实际上是 Capybara 尝试启动它的服务器以在其中运行应用程序,并验证服务器是否已启动。阻止这将阻止一切正常工作。如果您在使用 capybara 和 js: 时确实需要模拟一些请求:true 您可能应该查看 puffing-billy gem,它实现了与 capybara 驱动程序集成并允许此类事情的代理。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2012-02-03
      • 1970-01-01
      • 1970-01-01
      • 2018-11-28
      • 2022-01-22
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多