下面是我想要实现的一个最小示例:fn3()如果异步函数已fn2()解析(调用时fn1),我想测试被调用的情况。但是我以某种方式未能使用sinon的存根语法做到这一点。我想知道我误解了什么。
// System Under Test
export function fn1() {
fn2().then(() => {
fn3();
});
}
export async function fn2() {
return new Promise((resolve, reject) => {
// expensive work
resolve();
});
}
export function fn3() {
console.log("fn3");
}
// Test
import * as Page from "xxx";
it("test async", () => {
// stub this async to isolate the SUT
sinon.stub(Page, "fn2").resolves();
const stub = sinon.stub(Page, "fn3");
Page.fn1();
sinon.assert.calledOnce(stub);
});
/*
AssertError: expected fn3 to be called once but was called 0 times
276 | Page.fn1();
277 |
> 278 | sinon.assert.calledOnce(stub);
| ^
279 | });
280 | });
at Object.fail (node_modules/sinon/lib/sinon/assert.js:107:21)
at failAssertion (node_modules/sinon/lib/sinon/assert.js:66:16)
at Object.calledOnce (node_modules/sinon/lib/sinon/assert.js:92:13)
at Object.<anonymous> (src/test.test.tsx:278:22)
*/
九州编程
相关分类