Thursday, September 6, 2012

A Mock Sync For Backbone.js

If you want to do unit tests on a backbone.js application it would be really nice if you could replace the default sync with one that will not make an AJAX call but will return prebuilt data so you can test the application.

This is very easy to do. What you need to do is to replace the default Backbone.Sync object with one that will pull prebuilt data. This is what I have been using so far. It just takes an existing table of mock data and when a request comes in uses the URI as a key to retrieve the data and resets the data for the collection.

Obviously this only works for reading data, for Update, Delete etc things get a little more complex but there is no reason to not be able to expand this approach.

No comments:

Post a Comment