jasmine spy document

Jasmine Spies are test doubles that can act as stubs, spies, fakes or when used in an expecation, mocks. Spies should be created in test setup, before expectations. They can then be checked, using the standard Jasmine expectation syntax. Spies can be checked if they were called or not and what the calling params were. A Spy has the following fields: wasCalled, callCount, mostRecentCall, and argsForCall (see docs). Spies are torn down at the end of every spec. Note: Do not call new jasmine.Spy() directly - a spy must be created using spyOn, jasmine.createSpy or jasmine.createSpyObj.
// a stub
var myStub = jasmine.createSpy('myStub');  // can be used anywhere

// spy example
var foo = {
  not: function(bool) { return !bool; }
}

// actual foo.not will not be called, execution stops
spyOn(foo, 'not');

 // foo.not spied upon, execution will continue to implementation
spyOn(foo, 'not').andCallThrough();

// fake example
var foo = {
  not: function(bool) { return !bool; }
}

// foo.not(val) will return val
spyOn(foo, 'not').andCallFake(function(value) {return value;});

// mock example
foo.not(7 == 7);
expect(foo.not).toHaveBeenCalled();
expect(foo.not).toHaveBeenCalledWith(true);

 

Reference: http://pivotal.github.com/jasmine/jsdoc/symbols/jasmine.Spy.html

posted on 2012-05-04 11:17  Jalen Wang  阅读(415)  评论(0编辑  收藏  举报

导航