Nodejs,无服务器预期存根函数将被调用一次,但被调用0次

问题描述 投票:1回答:1

我在我的测试函数中尝试从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': () => {},
  },
};
node.js unit-testing sinon serverless-framework
1个回答
0
投票

您提供的代码样本中永远不会调用存根函数。如果您实际上使用[]

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);
  });
});

测试应按预期通过。

© www.soinside.com 2019 - 2024. All rights reserved.