使用端口范围制作网络模拟

Make a webmock with port range

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

     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'})

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

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