3

在 features/support/webmock.rb 中,我有

stub_request(:get, /(http\:\/\/catalog\.viglink\.com\/vigcatalog\/products\.xml\?include_identifiers=true&key=.*&keyword_upc=628586348097&results_per_page=20)/).
with(:headers => {'Accept'=>'*/*; q=0.5, application/xml', 'User-Agent'=>'Ruby'}).
to_return(:status => 200, :body => File.open('spec/support/628586348097.txt'))

我有两个黄瓜场景应该调用这个存根。在一种情况下,存根被识别,并且测试通过。在另一种情况下,我收到以下信息:

Real HTTP connections are disabled. Unregistered request: GET http://catalog.viglink.com/vigcatalog/products.xml?include_identifiers=true&key=key&keyword_upc=628586348097&results_per_page=20 with headers {'Accept'=>'*/*; q=0.5, application/xml', 'Accept-Encoding'=>'gzip, deflate', 'User-Agent'=>'Ruby'}

You can stub this request with the following snippet:

stub_request(:get, "http://catalog.viglink.com/vigcatalog/products.xml?include_identifiers=true&key=key&keyword_upc=628586348097&results_per_page=20").
with(:headers => {'Accept'=>'*/*; q=0.5, application/xml', 'Accept-Encoding'=>'gzip, deflate', 'User-Agent'=>'Ruby'}).
to_return(:status => 200, :body => "", :headers => {})

关于为什么 webmock 无法识别存根请求的任何建议?

4

1 回答 1

3

在 webmock.rb 文件中,确保将 stub_requests 放在 Before 块中。否则,您需要将它们包含在您的步骤中...

require 'webmock/cucumber'

Before do
  WebMock.disable_net_connect! #A precaution to avoid webmock making real http calls

  stub_request(:get, /.*url*/).to_return(:body => File.new("#{::Rails.root}/support/webmock/listing.json")
end
于 2014-08-21T18:31:06.173 回答