我在使用 sinon.js(和 mocha)进行测试时出错。当我通过 npm 运行所有测试脚本时会发生错误,但当我通过 IDE 运行单个脚本时不会发生错误。单独运行测试脚本没问题,测试通过。
即我有一个目录,里面有几个测试脚本。当我自己运行一个脚本时,测试通过了。当我运行目录中的所有脚本时,tess 失败并出现错误:
测试将失败
TypeError: Attempted to wrap getVariable which is already wrapped
当其他测试失败时:
TypeError: Cannot read property 'restore' of undefined
两个测试脚本都以相同的代码开头:
const
assert = require('assert'),
sinon = require('sinon');
global.context = {
getVariable: function(s) {}
};
var contextGetVariableMethod;
beforeEach(function () {
contextGetVariableMethod = sinon.stub(context, 'getVariable');
});
afterEach(function () {
contextGetVariableMethod.restore();
});
我想 mocha 正在同时运行两个测试?而且这些测试相互干扰。我很困惑为什么测试的范围不是独立的......也许是 global
的使用?
谢谢
最佳答案
根据 Sinon JS 文档,如果你有 var stub = sinon.stub(object, "method");
您必须使用 object.method.restore();
因此,在您的情况下:
afterEach(function () {
context.getVariable.restore()
});
关于javascript - 诗乃类型错误 : Attempted to wrap <method> which is already wrapped when running multiple scripts,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44721299/