This gem provides:
- jQuery Mockjax 1.5.2
- Ajax mocking helper in your Rspec integration tests
- Rails 3.1 or later
- Assets pipeline enabled
- jQuery
- Rspec/Capybara integration tests
Add the jquery_mockjax_rails
gem to your Gemfile
group :test do
gem 'jquery_mockjax_rails'
end
And run bundle install
Stub the ajax request in a before
block
describe 'My integration spec', js: true do
before do
stub_ajax url: '/api/path', responseText: { message: 'message' }
end
end
Once the browser has been lauched, open the JS console and test the mocked response
$.getJSON('/api/path', function(data) { console.log(data.message) }); # => message
The original idea has come from ejholmes, however, the gem has been deprecated and does not work with current Rails 3 application using rspec/capybara integration test, so I've decided to re-write the gem from scratch. Most of the core code pieces have been emerged from ejholmes's work.
Pull requests are more than welcome. Before submitting pull requests, please make sure your changes are covered with tests.