To understand the concept of behavior checking, let's revisit an example presented in Chapter 3, Testing Frontend Code, and test the observable behavior of NewInvestmentView
:
describe("and when an investment is created", function() { var callbackSpy; var investment; beforeEach(function() { callbackSpy = jasmine.createSpy('callback'); view.onCreate(callbackSpy); investment = view.create(); }); it("should invoke the 'onCreate' callback with the investment",function() { expect(callbackSpy).toHaveBeenCalled(); expect(callbackSpy).toHaveBeenCalledWith(investment); }); });
During the spec setup, it creates a new Jasmine Spy using the jasmine.createSpy
function while passing a name for it (callback
), then it sets this spy as an observer of the View's create event using the onCreate
function, and finally it invokes the create
function to create a new investment.
Later on at the expectations, the spec uses the matcher's toHaveBeenCalled
and toHaveBeenCalledWith...