Hadiyah Mujhid
Hadiyah Mujhid

Reputation: 187

Webmock not responding to stub_request

In features/support/webmock.rb, I have

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

I have two cucumber scenarios in which this stub should be called. In one scenario, the stub is recognized, and the test passes. In the other scenario, I receive the following:

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 => {})

Any suggestions as to why webmock is not recognizing the stub request?

Upvotes: 3

Views: 3117

Answers (1)

Elizabeth
Elizabeth

Reputation: 31

In the webmock.rb file, be sure to put the stub_requests in a Before block. Otherwise, you'll need to include them in your steps...

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

Upvotes: 3

Related Questions