Reputation: 4870
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?
Upvotes: 10
Views: 13205
Reputation: 8873
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.
Upvotes: 6
Reputation: 38418
Capybara isn't aware of any requests stubbed via webmock as the requests are coming from capybara-webkit or firefox.
This thoughtbot article will guide you through it:
http://robots.thoughtbot.com/using-capybara-to-test-javascript-that-makes-http
Upvotes: 4
Reputation: 5192
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.
Upvotes: 8