A Spy by itself is very useful, but its true power comes by changing an object's original implementation by using a counterpart Spy.
Consider the following example, which wants to validate that when the form is submitted, the create
function of view
has to be called.
describe("and when the form is submitted", function() { beforeEach(function() { spyOn(view, 'create'), view.$el.submit(); }); it("should create an investment", function() { expect(view.create).toHaveBeenCalled(); }); });
Here we make use of the global Jasmine function spyOn
to change the create
function of view
by a spy.
Then later at the spec, we use the toHaveBeenCalled
Jasmine matcher, to validate that the view.create
function was called.
After the spec is done, Jasmine restores the object's original behavior.