Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Stubbing Ajax requests in Cucumber / Capybara / Selenium?

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?

like image 798
Pascal Lindelauf Avatar asked Mar 15 '12 10:03

Pascal Lindelauf


2 Answers

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.

like image 177
iafonov Avatar answered Sep 18 '22 17:09

iafonov


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.

like image 39
Thibaut Barrère Avatar answered Sep 18 '22 17:09

Thibaut Barrère