If you are trying to use the updated version of the gem, consider using the documentation located on [jasmine.github.io](http://jasmine.github.io/2.0/ajax.html) as it is more up-to-date.
Download [mock-ajax.js](http://cloud.github.com/downloads/pivotal/jasmine-ajax/mock-ajax.js) and add it to your project. If you are using the jasmine gem, be sure the location you put mock-ajax.js is included in your src_files path in jasmine.yml. If you are using Jasmine standalone, make sure you add it to your spec runner.
Setup
---
Using the library in your Jasmine specs consists of four parts:
4. Inspecting Ajax requests and setting expectations on them
Example
---
Let's use a simple Foursquare venue search app to show each of these steps.
### 1. Defining Test Responses ###
After signing up for an API key and playing around with curl a bit you should have an idea of what API resources you are interested in and what sample responses look like. Once you do, you can define simple JavaScripts objects that will be used to build XMLHttpRequest objects later.
For example, if you have a response that looks like this:
{
"meta":{
"code":200,
"errorType":"deprecated",
"errorDetail":"This endpoint will stop returning groups in the future. Please use a current version, see http://bit.ly/lZx3NU."
A good place to define this is in `spec/javascripts/helpers/test_responses`. You can also define failure responses, for whatever status codes the API you are working with supports.
### 2. Installing the mock ###
Install the mock using `jasmine.Ajax.useMock()`:
beforeEach(function() {
jasmine.Ajax.useMock();
...
After this, all Ajax requests will be captured by jasmine-ajax. If you want to do things like load fixtures, do it before you install the mock (see below).
Before you can specify that a request uses your test response, you must have a handle to the request itself. This means that the request is made first by the code under test and then you will set your test response (see next step).
foursquare.search('40.019461,-105.273296', {
onSuccess: onSuccess,
onFailure: onFailure
});
request = mostRecentAjaxRequest();
The onreadystatechange event isn't fired to complete the ajax request until you set the response in the next step.
Now that you've defined some test responses and installed the mock, you need to tell jasmine-ajax which response to use for a given spec. If you want to use your success response for a set of related success specs, you might use:
describe("on success", function() {
beforeEach(function() {
request.response(TestResponses.search.success);
});
Now for all the specs in this example group, whenever an Ajax response is sent, it will use the `TestResponses.search.success` object defined in your test responses to build the XMLHttpRequest object.
Putting it all together, you can install the mock, pass some spies as callbacks to your search object, and make expectations about the expected behavior.
Most third-party Jasmine extensions use Ajax to load HTML fixtures into the DOM. Since jasmine-ajax intercepts all Ajax calls after it is installed, you need to load your fixtures before installing the mock. If you are using jasmine-jquery, that looks like this: