【发布时间】:2019-09-22 02:12:19
【问题描述】:
我有多个文件使用 sinon 来存根相同的方法 Utils.getTimestamp。
运行测试文件时,一次一个,所有测试都通过。 一次运行测试文件时,测试失败:TypeError: "Attempted to wrap getTimestamp which has been packed"
在这两个文件中,我都有带有前后块的描述块
在 Before 块中,我将以下方法存根: getTimestampStub= sinon.stub(Utils, 'getTimestamp') .returns(myTimestamp);
在 After 块中,我恢复了如下方法: getTimestampStub.restore();
我根据这个答案尝试了这个: https://stackoverflow.com/a/36075457/6584537
示例文件:
文件 1
describe("First Stub", () => {
let getTimestampStub;
before(() => {
getTimestampStub= sinon.stub(Utils, 'getTimestamp') .returns("SOME_TIMESTAMP");
});
it("Should run some code that uses getTimestamp", () => {
// Some code that in the process uses `Utils.getTimestamp`
});
after(() => {
getTimestampStub.restore();
});
});
文件 2
describe("Second Stub", () => {
let getTimestampStub;
before(() => {
getTimestampStub= sinon.stub(Utils, 'getTimestamp') .returns("SOME_TIMESTAMP");
});
it("Should run some OTHER code that uses getTimestamp", () => {
// Some code that in the process uses `Utils.getTimestamp`
});
after(() => {
getTimestampStub.restore();
});
});
【问题讨论】:
标签: unit-testing mocking mocha.js sinon stub