Skip to content

Instantly share code, notes, and snippets.

@gbabiars
Created July 11, 2013 14:20
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save gbabiars/5975858 to your computer and use it in GitHub Desktop.
Save gbabiars/5975858 to your computer and use it in GitHub Desktop.
An example of how to unit test caching within a function that only makes one
test('calling myFunc twice with the same url should only make one request to that url', function() {
var fakeServer = sinon.fakeServer.create(), // initialize fake server
myModule = myModule(), // whatever you need to do to instantiate your system under test
url = '/user/12',
callback = function() {}; // does nothing
fakeServer.autoRespond = true; // make sure the fake response is sent after every request
fakeServer.respondWith('GET', url,
[200, { 'Content-Type': 'application/json' }, '[{ "id": 12, "name": "John Doe" }]']); // define your fake response that the url will send
// call to get user twice
myModule.getUser(url, callback);
myModule.getUser(url, callback);
equal(fakeServer.requests.length, 1, 'Should have made 1 request'); // verify that only one xhr request was made
fakeServer.restore(); // allow future xhr requests to actually occur
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment