【发布时间】:2019-05-13 18:50:08
【问题描述】:
我有 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 has already been spied upon。有人可以解释为什么即使间谍在不同的测试套件中我也会收到此错误以及如何修复它。
【问题讨论】:
-
你为什么要监视和测试原生 JS/浏览器代码?我认为这些函数在被调用时会起作用是安全的。
-
因为test没有加载HTML文件,所以js文件中的
document.getElementById('').style等会报错。 -
您可能应该测试某个元素是否存在,而不是测试是否调用了本机浏览器代码。
标签: javascript jasmine karma-jasmine jasmine-jquery jasmine2.0