我想测试是否从另一个方法(方法2)调用本地方法(方法1)。我尝试了这样的方法,但它不起作用,因为method1()仍然有的原始定义。该代码片段看起来是这样的:使用茉莉花如何检查本地方法是否被调用?
var ClassA = function() {
var method1 = function() {
console.log('method1');
};
var method2 = function() {
method1();
};
return {method1: method1, method2: method2}
}
测试用例:
it("should call method1 when method2 is called", function() {
var objectA = new ClassA();
spyOn(objectA, 'method1').andCallThrough;
objectA.method2();
expect(objectA, 'method1').toHaveBeenCalled();
});
试图重写方法1到没有任何成功:
objectA.method1 = jasmine.createSpy('aSpy').andCallThrough();
你的代码看起来不错,但是,你在'spyOn(objectA,'method1')。和.CallThrough''行中缺少'()'。 –