制作带端口范围的webmock

时间:2015-09-29 15:49:23

标签: regex rspec capybara webmock

我在js: true中使用了describe的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'})

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

1 个答案:

答案 0 :(得分:1)

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