If you are using the updated version of this library, there is some additional documentation located at [jasmine.github.io](http://jasmine.github.io/2.0/ajax.html) that is up-to-date.
Download [mock-ajax.js](https://raw.github.com/pivotal/jasmine-ajax/master/lib/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.
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.
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).
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.
By default the `data` function is very naive about parsing form data being sent.
The provided parsers are:
1. If the XHR has a content-type of application/json, JSON.parse
1. Otherwise simply split query string by '&' and '='
If you need more control over how your data is presented, you can supply a custom param parser. Custom parsers will be prepended to the list of parsers to try.
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: