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.
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
nullis 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.
handlergets called with the can.ajax 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.fixtureintercepts 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
GETrequest to/recipes:Requests made to
GET /recipeswith 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.jsonand 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.jsonand 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
nullfor the fixture option:You can also set on to false:
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
responseis 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/fixtureand defines all your fixtures. For example, if you have a 'todo' application, you might havetodo/fixtures/fixtures.jslook like:Notice: We used steal's ignore option to prevent loading the fixture plugin in production.
Finally, we steal
todo/fixtures/fixtures.jsin 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: