In my Rails 3.2 project, I send an occasional http request to http://jsonip.com
from Javascript. But when running Cucumber with the @javascript
tag, the request gets sent very frequently. This is obviously very undesirable and I would like to stub such requests. Now, I thought I could use the stub_request
from webmock
for this, like so:
Before do
stub_request(:any, /.*jsonip.*/).to_return(:body => '{"ip":"24.104.73.2","about":"/about"}')
end
but even with this in place, jsonip
gets called from Javascript. And so I found that webmock
does not actually stub Ajax request (at least not with the above stub_request
statement).
What is the right way to stub Ajax request in a Cucumber / Capybara / Selenium setup?
The only way to do it is to change url or disable request depending on the environment application is run.
You can't stub this request from test side because this request is made by browser not by your application.
Also as temporary solution or solution for CI server you can disable this url in hosts file.
You can use puffing-billy https://github.com/oesmith/puffing-billy for that purpose. I'm using it to mock JSONP calls to Recurly in my app.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With