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

转载于:https://www.cnblogs.com/jalenwang/archive/2012/05/04/jasmine-spy.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值