3

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?

Hadiyah Mujhid
  • 177
  • 1
  • 2
  • 10
  • Is it exact text that WebMock prints out? It's weird that it prints "Unregistered request" with "key=989aisdofaisjdf9asd08f" and "You can stub thus request with the following snippet" with "key=key" – Bartosz Blimke Nov 07 '13 at 16:48
  • no, that was me editing the post, trying to remove the actually key from this post, but I forgot to remove it in all places....but its prints out the same in the same in the console. – Hadiyah Mujhid Nov 07 '13 at 17:50
  • 1
    Can you please make sure the stub is loaded? It doesn't look it is. If the stub was loaded, you would see the list of registered stubs, under the suggested stub instructions. – Bartosz Blimke Nov 08 '13 at 10:01
  • Thanks! This was the problem. It was not registering the stub_requests in the webmock file. The only way I could get this to work was to move it into the step definitions. Not sure why it worked for one feature but not the other. But either way its working now. Thanks again! – Hadiyah Mujhid Nov 08 '13 at 20:17
  • 2
    @HadiyahMujhid you should add the solution you found as an answer so others know this is resolved. Or delete it if you don;t think it's useful to others. – toxaq Mar 27 '14 at 14:08

1 Answers1

3

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
Elizabeth
  • 31
  • 3