$.fixture(settings, fixture) -> undefined
{Object|String}
Configures the AJAX requests the fixture should intercept. If an object is passed, the object's properties and values are matched against the settings passed to $.ajax.
If a string is passed, it can be used to match the url and type. Urls
can be templated, using {NAME} as wildcards.
{Function|String}
The response to use for the AJAX request. If a string url is passed, the ajax request is redirected to the url. If a function is provided, it looks like:
fixture( originalSettings, settings, headers )
where:
If null is passed, and there is a fixture at settings, that fixture will be removed, allowing the AJAX request to behave normally.
{undefined}
$.fixtureintercepts a AJAX request and simulates the response with a file or function. They are a great technique when you want to develop JavaScript independently of the backend.Types of Fixtures
There are two common ways of using fixtures. The first is to map Ajax requests to another file. The following intercepts requests to
/tasks.jsonand directs them tofixtures/tasks.json:The other common option is to generate the Ajax response with a function. The following intercepts updating tasks at
/tasks/ID.jsonand responds with updated data:We categorize fixtures into the following types:
There are different ways to lookup static and dynamic fixtures.
Static Fixtures
Static fixtures use an alternate url as the response of the Ajax request.
Dynamic Fixtures
Dynamic Fixtures are functions that get the details of the Ajax request and return the result of the mocked service request from your server.
For example, the following returns a successful response with JSON data from the server:
The fixture function has the following signature:
where the fixture function is called with:
and the fixture function returns an array as arguments for ajaxTransport's
completeCallbackwith:However, $.fixture handles the common case where you want a successful response with JSON data. The previous can be written like:
If you want to return an array of data, wrap your array in another array:
$.fixture works closesly with jQuery's ajaxTransport system. Understanding it is the key to creating advanced fixtures.
Templated Urls
Often, you want a dynamic fixture to handle urls for multiple resources (for example a REST url scheme). $.fixture's templated urls allow you to match urls with a wildcard.
The following example simulates services that get and update 100 todos.
Notice that data found in templated urls (ex:
{id}) is added to the original data object.Simulating Errors
The following simulates an unauthorized request to
/foo.This could be received by the following Ajax request:
Turning off Fixtures
You can remove a fixture by passing
nullfor the fixture option:You can also set [jQuery.fixture.on $.fixture.on] to false:
Make
$.fixture.make makes a CRUD service layer that handles sorting, grouping, filtering and more.
Testing Performance
Dynamic fixtures are awesome for performance testing. Want to see what 10000 files does to your app's performance? Make a fixture that returns 10000 items.
What to see what the app feels like when a request takes 5 seconds to return? Set [jQuery.fixture.delay] to 5000.
Demo
HTML
Source