除了在Jasmine上监视它之外,我想要一个函数。我怎么能这样做?
var o = { foo: function(){} };
var spy = spyOn(o, 'foo')
.andStubWith(function() { console.log('foo'); }); // This is pseudocode - is there a real equivalent?
我不想在我的测试中简单地覆盖该功能的原因是IIUC,Jasmine将在每次测试后撤消任何间谍。
答案 0 :(得分:3)
请参阅http://jasmine.github.io/2.0/introduction.html#section-Spies:_and.callFake
spyOn(o, "foo").and.callFake(function() {
console.log('foo')
return 1001;
});