1

我有一个控制器测试,我正在测试一种方法,该方法也尝试使用 Braintree 处理付款。我尝试将假 Braintree 添加到项目中,但是当我运行测试时,webmock 告诉我我需要存根这个请求:

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

You can stub this request with the following snippet:

stub_request(:get, "http://127.0.0.1:57793/__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 => {})

由于端口号不同,我在 stub_request 中为 url 参数使用正则表达式:

  stub_request(:get, /http:\/\/127\.0\.0\.1:\d{5}\/__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 => {})

不幸的是,当我运行测试时,我仍然收到来自 webmock 的消息,说我需要存根请求。

4

0 回答 0