如何监视不是对象方法的函数。在我的情况下,callMe
也不在window
对象上 - 它是通过 Angular 加载的依赖项。
if (X) {
callMe('hello');
}
最佳答案
您可以使用jasmine.createSpy
方法创建一个 spy 对象。鉴于 spy 对象是常规 JavaScript 对象,您可以使用此 spy 对象来覆盖您的 callMe
函数,以便监视它。
function callMe() {
// implementation
}
describe('an example block', function() {
it('creates a spy', function() {
callMe = jasmine.createSpy('callMe');
callMe();
expect(callMe).toHaveBeenCalled();
});
});
When there is not a function to spy on,
jasmine.createSpy
can create a “bare” spy. This spy acts as any other spy – tracking calls, arguments, etc. But there is no implementation behind it. Spies are JavaScript objects and can be used as such.
关于angularjs - Jasmine spyOn 函数不是对象的方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39298815/