Created
July 11, 2013 14:20
-
-
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
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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