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:
1. Defining test responses
2. Installing the mock
3. Defining the response for each request
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).
### 3. Set responses ###
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.
### 4. Inspect Ajax requests ###
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.
describe("FoursquareVenueSearch", function() {
var foursquare, request;
var onSuccess, onFailure;
beforeEach(function() {
jasmine.Ajax.useMock();
onSuccess = jasmine.createSpy('onSuccess');
onFailure = jasmine.createSpy('onFailure');
foursquare = new FoursquareVenueSearch();
foursquare.search('40.019461,-105.273296', {
onSuccess: onSuccess,
onFailure: onFailure
});
request = mostRecentAjaxRequest();
});
describe("on success", function() {
beforeEach(function() {
request.response(TestResponses.search.success);
});
it("calls onSuccess with an array of Locations", function() {
expect(onSuccess).toHaveBeenCalled();
var successArgs = onSuccess.mostRecentCall.args[0];
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: