Sinon 、递归和 setTimeout
Sinon , recursion and setTimeout
我通过 Whosebug 查看了这个问题,但似乎大多数问题都涵盖了三个中的两个,而且大多数人不需要同时使用这三个。
这是代码片段。我试图让 testA
在一定超时后递归调用自身。
const testA = async () => {
setTimeout(() => {
testA();
}, 1000);
return;
};
这是我的测试代码:
//test.js
const someThing = require("../tester.js");
const chai = require("chai");
const sinonChai = require("sinon-chai");
const sinon = require("sinon");
chai.use(sinonChai);
describe("Test A", () => {
it("Should call twice", () => {
const clock = sinon.useFakeTimers();
const testASpy = sinon.spy(someThing, "testA");
testASpy();
chai.expect(testASpy).to.have.been.calledOnce; //is fine
clock.tick(1000);
chai.expect(testASpy).to.have.been.calledTwice; //fails
});
});
我一直看到每个人都在说 "Sinon cannot stub a standalone function",但我找不到原因。如果有人能指出我阅读更多相关内容的方向,我真的很想看到。同时,如果有人知道解决此问题的方法,我也很想知道更多。再次感谢!
你的代码没有意义。会造成死循环。这是单元测试解决方案:
index.ts
:
export const testA = async () => {
setTimeout(() => {
console.count("testA");
testA();
}, 1000);
};
index.spec.ts
:
import * as mod from "./";
import sinon from "sinon";
import { expect } from "chai";
describe("58843454", () => {
it("should pass", async () => {
const clock = sinon.useFakeTimers();
const testASpy = sinon.spy(mod, "testA");
await testASpy();
expect(testASpy.calledOnce).to.be.true;
clock.tick(1000);
expect(testASpy.calledTwice).to.be.true;
});
});
100% 覆盖率的单元测试结果:
58843454
testA: 1
✓ should pass
1 passing (16ms)
---------------|----------|----------|----------|----------|-------------------|
File | % Stmts | % Branch | % Funcs | % Lines | Uncovered Line #s |
---------------|----------|----------|----------|----------|-------------------|
All files | 100 | 100 | 100 | 100 | |
index.spec.ts | 100 | 100 | 100 | 100 | |
index.ts | 100 | 100 | 100 | 100 | |
---------------|----------|----------|----------|----------|-------------------|
源代码:https://github.com/mrdulin/mocha-chai-sinon-codelab/tree/master/src/Whosebug/58843454
我通过 Whosebug 查看了这个问题,但似乎大多数问题都涵盖了三个中的两个,而且大多数人不需要同时使用这三个。
这是代码片段。我试图让 testA
在一定超时后递归调用自身。
const testA = async () => {
setTimeout(() => {
testA();
}, 1000);
return;
};
这是我的测试代码:
//test.js
const someThing = require("../tester.js");
const chai = require("chai");
const sinonChai = require("sinon-chai");
const sinon = require("sinon");
chai.use(sinonChai);
describe("Test A", () => {
it("Should call twice", () => {
const clock = sinon.useFakeTimers();
const testASpy = sinon.spy(someThing, "testA");
testASpy();
chai.expect(testASpy).to.have.been.calledOnce; //is fine
clock.tick(1000);
chai.expect(testASpy).to.have.been.calledTwice; //fails
});
});
我一直看到每个人都在说 "Sinon cannot stub a standalone function",但我找不到原因。如果有人能指出我阅读更多相关内容的方向,我真的很想看到。同时,如果有人知道解决此问题的方法,我也很想知道更多。再次感谢!
你的代码没有意义。会造成死循环。这是单元测试解决方案:
index.ts
:
export const testA = async () => {
setTimeout(() => {
console.count("testA");
testA();
}, 1000);
};
index.spec.ts
:
import * as mod from "./";
import sinon from "sinon";
import { expect } from "chai";
describe("58843454", () => {
it("should pass", async () => {
const clock = sinon.useFakeTimers();
const testASpy = sinon.spy(mod, "testA");
await testASpy();
expect(testASpy.calledOnce).to.be.true;
clock.tick(1000);
expect(testASpy.calledTwice).to.be.true;
});
});
100% 覆盖率的单元测试结果:
58843454
testA: 1
✓ should pass
1 passing (16ms)
---------------|----------|----------|----------|----------|-------------------|
File | % Stmts | % Branch | % Funcs | % Lines | Uncovered Line #s |
---------------|----------|----------|----------|----------|-------------------|
All files | 100 | 100 | 100 | 100 | |
index.spec.ts | 100 | 100 | 100 | 100 | |
index.ts | 100 | 100 | 100 | 100 | |
---------------|----------|----------|----------|----------|-------------------|
源代码:https://github.com/mrdulin/mocha-chai-sinon-codelab/tree/master/src/Whosebug/58843454