【问题标题】:Nodejs, Serverless expected stub function to be called once but was called 0 timesNodejs,无服务器预期存根函数被调用一次但被调用0次
【发布时间】:2020-04-21 16:40:36
【问题描述】:

在我的测试函数中尝试从 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】:

    在您提供的代码示例中从未调用存根函数。如果你真的用

    调用函数
    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);
      });
    });
    

    测试应该按预期通过。

    【讨论】:

      猜你喜欢
      • 2018-11-14
      • 2020-09-15
      • 2020-03-28
      • 1970-01-01
      • 1970-01-01
      • 2021-11-05
      • 2016-01-09
      • 1970-01-01
      • 2017-01-03
      相关资源
      最近更新 更多