【问题标题】:How to stub AWSXray.captureHTTPsGlobal using Sinon?如何使用 Sinon 存根 AWSXray.captureHTTPsGlobal?
【发布时间】:2019-07-12 08:10:13
【问题描述】:

在我的 nodejs lambda 函数中,我使用 AWSXray 来简单地捕获该函数进行的 https 调用。

const AWSXRay = require("aws-xray-sdk-core");
AWSXRay.captureHTTPsGlobal(require("https"));

在我的单元测试中,我如何模拟这个?

我试过用 sinon 模拟它:

before(async () =>
  await sandbox.stub(AWSXRay, 'captureHTTPsGlobal').returns({})
);

after(async () => {
  await sandbox.restore();
});

在测试中出现错误:

OperationalError: Failed to get the current sub/segment from the context.
    at Object.contextMissingRuntimeError [as contextMissing]

【问题讨论】:

    标签: node.js unit-testing aws-lambda sinon aws-xray


    【解决方案1】:

    问题不在于 Xray,而在于我编写测试的方式。

    AWSXray 将尝试捕获所有 https 调用。但是这种情况在理想情况下不应该出现在测试中,因为在理想的测试套件中,所有 http/https 调用无论如何都应该被存根。

    我通过正确模拟进行 https 调用的单元解决了这个问题。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2015-10-13
      • 2015-08-18
      • 1970-01-01
      • 1970-01-01
      • 2018-10-15
      • 2017-05-04
      • 1970-01-01
      • 2014-07-17
      相关资源
      最近更新 更多