Skip to content

Instantly share code, notes, and snippets.

@dbouwman
Created July 26, 2013 13:22
Show Gist options
  • Save dbouwman/6088799 to your computer and use it in GitHub Desktop.
Save dbouwman/6088799 to your computer and use it in GitHub Desktop.
Example Jasmine Async Test
describe("Example Async Test", function() {
var value, flag;
it("should do something that has a callback", function() {
//Specs are written by defining a set of blocks with calls to runs,
//which usually finish with an asynchronous call.
var ourObj = new SomeObject();
runs(function() {
flag = false;
//simple callback that will set our "flag"
var flagCallback = function(result){
flag = true;
};
//call do a function that does some async
//work, and then calls the callback
ourObj.doSomething(flagCallback);
});
//The waitsFor block takes a latch function, a failure message, and a timeout.
//The latch function polls until it returns true or the timeout expires,
//whichever comes first. If the timeout expires, the spec fails with the error message.
waitsFor(function() {
return flag;
}, "the call to return", 750);
//Once the asynchronous conditions have been met, another runs block
//defines final test behavior. This is usually expectations based on
//state after the asynch call returns.
runs(function() {
//assertions
expect(outObj.someValue).toBeGreaterThan(0);
});
});
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment