我有 3 个测试,每个测试不同的方法。
it('test function1', function() {
spyOn(document, 'getElementById');
// ... some code to test function1
expect(document.getElementById).toHaveBeenCalled();
});
it('test function2', function() {
spyOn(document, 'getElementById');
// ... some code to test function2
expect(document.getElementById).toHaveBeenCalled();
});
it('test function3', function() {
spyOn(document, 'getElementById');
// ... some code to test function3
expect(document.getElementById).toHaveBeenCalled();
});
但是当我运行这些测试时,我收到以下错误:getElementById 已被监视
。有人可以解释为什么即使 spy 在不同的测试套件中我也会收到此错误以及如何修复它。
最佳答案
回复晚了,但是,如果有人试图多次监视相同的函数但返回值不同,你可以使用
it('test function', function() {
// spy and return data
spyOn(serviceName,'functionName').and.returnValue(data);
expect(serviceName.functionName).toHaveBeenCalled();
// spy and return newData
serviceName.functionName = jasmine.createSpy().and.returnValue(newData);
expect(serviceName.functionName).toHaveBeenCalled();
});
关于javascript - 如何修复功能已在 Jasmine 中发现错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56118277/