can.fixture
Simulate AJAX requests.
Note: can.fixture depends on the can.object plugin. If you are not using the AMD or Steal version of CanJS you need to include can.object.js before can.fixture.
In the following CanJS community hangout we also talk about CanJS models and fixtures:
can.fixture( url, toUrl )
Trap requests from one url and redirect them from another.
Parameters
-
url
{String}
Trap requests made by can.ajax to this url.
-
toUrl
{String | null}
Redirect requests to this url. If
null
is provided, this removes a the fixture aturl
.
can.fixture( url, handler(request, response, requestHeaders) )
Trap requests to a url and provide the response with a callback function.
Parameters
-
url
{String}
Trap requests made by can.ajax to this url.
The url can be templated with tags that look like
{TEMPLATE}
. For example: "/users/{id}". Any templated values get added to thehandler
's request object's data. -
handler
{requestHandler(request, response, requestHeaders)}
Specifies the response of the fixture.
handler
gets called with the can.ajax [can.AjaxSettings settings object] and a response handler that is used to specify the response.
can.fixture(fixtures)
Configures multiple ajax traps.
Parameters
-
fixtures
{Object<url,requestHandler(request, response, requestHeaders) | String>}
An mapping of templated urls to redirect urls or request handler functions.
can.fixture({ "/tasks": "/fixtures/tasks.json", "DELETE /tasks/{id}": function(){ return {}; } })
Use
can.fixture
intercepts an AJAX request and simulates the response with a file or function. Use them to develop JavaScript independently of the backend services.The following simulates a
GET
request to/recipes
:Requests made to
GET /recipes
with can.ajax, jQuery.ajax,or jQuery.get will receive the data returned by the fixture function above:There are two common ways of using fixtures. The first is to map Ajax requests to another file. The following intercepts requests to
/tasks.json
and directs them tofixtures/tasks.json
:The other common option is to generate the Ajax response with a requestHandler function. The following intercepts updating tasks at
/tasks/ID.json
and responds with updated data:A requestHandler function's response argument can be used to specify even more details of the Ajax response:
Read more about requestHandler and its response argument on their own documentation pages.
Templated Urls
Often, you want a dynamic fixture to handle urls for multiple resources (for example a REST url scheme). can.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 request's 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
null
for the fixture option:You can also set on to false:
Bypassing Fixtures
While there are few cases where you would need to, it is possible to bypass a fixture completely, without turning off fixtures globally. This is done by passing
fixture: false
to your AJAX settings. This will preventcan.fixture
from trapping your request, and actually send it to the server.can.fixture.store
store makes a CRUD service layer that handles sorting, grouping, filtering and more. Use it with a can.Model like this:
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 delay to 5000.
Since
response
is called asynchronously you can also set a custom fixture timeout like this:Organizing fixtures
The best way of organizing fixtures is to have a 'fixtures.js' file that steals
can/util/fixture
and defines all your fixtures. For example, if you have a 'todo' application, you might havetodo/fixtures/fixtures.js
look like:Notice: We used steal's ignore option to prevent loading the fixture plugin in production.
Finally, we steal
todo/fixtures/fixtures.js
in the app file (todo/todo.js
) like:We typically keep it a one liner so it's easy to comment out.
Switching Between Sets of Fixtures
If you are using fixtures for testing, you often want to use different sets of fixtures. You can add something like the following to your fixtures.js file: