【问题标题】:Sinon test to check all methods were called in lambda用于检查所有方法的 Sinon 测试在 lambda 中调用
【发布时间】:2019-02-15 10:29:00
【问题描述】:

我的 AWS lambda 函数是这样的:

exports.handler = function(event, context, callback) {
  const myModel = exports.deps().myModel;
  return tools.checkPermission(event)
    .then((id) => myModel.create(JSON.parse(event.body), id))
    .then((campaign) =>
      tools.handleAPIResponse(
        callback,
        data,
        201,
        Object.assign({Location: event.path + '/' + data.id,}, tools.HEADERS)
      )
    ).catch(err => tools.handleAPIError(callback, err));
};

我正在使用 sinon.js 编写一个测试用例,只是为了检查我的 lambda 函数中的所有方法是否都通过存根所有函数来调用。喜欢

myModel.create
tools.checkPermission
tools.handleAPIError
tools.handleAPIResopnse

我正在像这样进行存根和测试:

it('should call all functions ', () => {
 const event = {};

 createMyStub = sinon.stub(myModel, 'create');
 createMyStub.withArgs(sinon.match.any).returns(Promise.resolve('Hello'));

 const checkPermission = sinon.stub(tools, 'checkPermission');
 checkPermission.withArgs(sinon.match.any).returns(Promise.resolve('user'));

 const handleAPIResponse = sinon.stub(tools, 'handleAPIResponse');
 handleAPIResponse.withArgs(sinon.match.any).returns('Done');

 const callback = sinon.spy();

 API.handler(event, {}, callback);
 expect(checkPermission.called).to.be(true);
 expect(handleAPIResponse.called).to.be(true);
 expect(createMyStub.called).to.be(true);

 createMyStub.restore();
 checkPermission.restore();
 handleAPIResponse.restore();
});

但我没有得到预期的结果。另外,当我不存根 tools.handleAPIResponse 时,如何查看回调的内容,并期望回调中的实际结果。

【问题讨论】:

  • 你的测试结果如何?

标签: javascript node.js unit-testing mocha.js sinon


【解决方案1】:

我在这部分的测试中发现了一个关键错误

API.handler(event, {}, callback);

该函数是异步函数,因此我们必须将其称为 promise,例如

await API.handler(event, {}, callback);

或者

API.handler(event, {}, callback).then(....)

我更喜欢前一种方法。并且还可以改进某些部分测试,例如使用sinon.resolvessinon.restore,如下所示:

it('should call all functions ', async () => { // specify `async`
  const event = {};

  createMyStub = sinon.stub(myModel, 'create');
  createMyStub.withArgs(sinon.match.any).resolves('hello'); // using `resolves`

  const checkPermission = sinon.stub(tools, 'checkPermission');
  checkPermission.withArgs(sinon.match.any).resolves('user')

  const handleAPIResponse = sinon.stub(tools, 'handleAPIResponse');
  handleAPIResponse.withArgs(sinon.match.any).returns('Done');

  const callback = sinon.spy();

  await API.handler(event, {}, callback); // specify `await`

  expect(checkPermission.called).to.be(true);
  expect(handleAPIResponse.called).to.be(true);
  expect(createMyStub.called).to.be(true);

  sinon.restore(); // this is sufficient in latest version of sinon, no need to restore on all methods
 });

关于您检查回调的问题,也许我们可以使用sinon.calledWith 例如:

  expect(handleAPIResponse.calledWith(callback, ...).to.be(true);

参考:

希望对你有帮助

【讨论】:

  • 我尝试使用这个解决方案,但是 expect(handleAPIResponse.call).to.be(true);期望(createMyStub.called).to.be(真);总是返回假。此外,在这个测试用例中存根这些函数会干扰实际工具对象的单元测试
  • @Bharthan 此行中的myModel 值是什么createMyStub = sinon.stub(myModel, 'create');
  • myModel 是一个访问数据库的类对象。所以myModel.create(JSON.parse(event.body), id) 在数据库中创建了一条新记录。如果成功,它会返回一个带有 data 作为内容插入的承诺。
  • @Bharthan 酷。在你的源中,myModel 来自const myModel = exports.deps().myModel;,那么在测试中呢?我没看到。
猜你喜欢
  • 2016-01-21
  • 1970-01-01
  • 2019-03-24
  • 2019-04-18
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多