【发布时间】:2021-08-29 18:26:46
【问题描述】:
我有以下代码:
it('Should return error', async () => {
const searchMetadata = {
details: {}
};
sandbox.mock(resultService).expects('doesSearchExists').atLeast(1).resolves(true);
sandbox.mock(resultService).expects('doesSearchExists').atLeast(1).withArgs("limit").resolves(false);
sandbox.mock(analyticsService).expects('getMetadata').throws(UNABLE_TO_CALCULATE_METADATA);
await GET(request, res, (result: any) => {
expect(result).to.be.deep.equal(UNABLE_TO_CALCULATE_METADATA)
});
});
如您所见,我试图根据我发送的参数两次不同地模拟相同的函数 doesSearchExists。如果发送了限制,dosSearchExists 应该返回 false,如果缺少限制,doSearchExists 应该返回 true。但是当我尝试它时,我得到了
TypeError: Attempted to wrap doesSearchExists which is already wrapped
知道如何实现上述目标吗?
【问题讨论】:
-
您需要存储模拟并在第二次设置中重复使用它。
标签: node.js unit-testing sinon