我正在尝试与Sinon一起使用库函数。抱歉,无法发布确切的代码,但看起来像下面的代码。
我正在尝试对library.js进行存根的库
async function a(input) {
// make some api calls here
returns input;
}
export async function b {
const output = await a(123);
// do some magic here with output
// call again a
returns a(output);
}
我的测试用例文件:sinon.js
import * as lib from 'library.js'
describe('', ()=>{
it('', ()=>{
sinon.stub(lib, 'b')
.withArgs(123)
.resolves(123)
})
})
当我运行此代码时,我的存根返回undefined
而不是预期的输出123
,有人可以帮助我找到我在这里做什么的错误吗?
我并没有声称自己是这个主题的专家,但在我看来,您正在尝试存根而不是模拟。尝试这样的事情。
describe('', () => {
it('', () => {
const mock = sinon.mock(lib);
mock
.expects('b').withArgs(123)
.resolves(345);
lib.b(123).then((c) => console.log(c));
})
})
这将打印345。可以肯定,这是一种更优雅的方法,但是可以使用。