Jasmine cheat sheet - createSpy
Demonstrating createSpy functionality
by munir
HTML
<script src="http://pivotal.github.io/jasmine/lib/jasmine-1.3.1/jasmine.js"></script>
<script src="http://pivotal.github.io/jasmine/lib/jasmine-1.3.1/jasmine-html.js"></script>
<link rel="stylesheet" href="http://pivotal.github.io/jasmine/lib/jasmine-1.3.1/jasmine.css">
JavaScript
describe("A bare spy created manually", function () {
var whatAmI;
beforeEach(function () {
// We are going to track calls to whatAmI
whatAmI = jasmine.createSpy('whatAmI-name');
whatAmI("I", "am", "a", "spy");
});
it("is named, which helps in error reporting", function () {
expect(whatAmI.identity).toEqual('whatAmI-name')
});
it("tracks that the spy was called", function () {
expect(whatAmI).toHaveBeenCalled();
});
it("tracks its number of calls", function () {
expect(whatAmI.calls.length).toEqual(1);
});
it("tracks all the arguments of its calls", function () {
expect(whatAmI).toHaveBeenCalledWith("I", "am", "a", "spy");
});
it("allows access to the most recent call", function () {
expect(whatAmI.mostRecentCall.args[0]).toEqual("I");
});
});
// Jasmine spec runner
(function () {
var jasmineEnv = jasmine.getEnv();
jasmineEnv.updateInterval = 1000;
var trivialReporter = new jasmine.TrivialReporter();
jasmineEnv.addReporter(trivialReporter);
jasmineEnv.specFilter = function (spec) {
return trivialReporter.specFilter(spec);
};
var currentWindowOnload = window.onload;
window.onload = function () {
if (currentWindowOnload) {
currentWindowOnload();
}
execJasmine();
};
function execJasmine() {
jasmineEnv.execute();
trivialReporter.outerDiv.className += ' show-passed';
}
})();