我在我的测试函数中尝试从sinon创建存根时遇到了上述错误。我正在尝试测试负责对其他端点进行http调用的函数。我试图了解为什么它无法解析提供的输出。
const sinon = require('sinon');
const sandbox = sinon.createSandbox();
describe('test endpoint', () => {
it('should be test function', async () => {
const stub = sinon.stub(someServiceMock.POST, '/funcName').resolves({ status: 204 });
sinon.assert.calledOnce(stub);
});
});
});
并获取AssertError: expected '/funcName' to be called once but was called 0 times
我在存根中传递的对象是
const someServiceMock = {
POST: {
'/funcName': () => {},
},
};
您提供的代码样本中永远不会调用存根函数。如果您实际上使用[]
describe('test endpoint', () => { it('should be test function', async () => { const stub = sinon.stub(someServiceMock.POST, '/funcName').resolves({ status: 204 }); someServiceMock.POST["/funcName"](); sinon.assert.calledOnce(stub); }); });
测试应按预期通过。