【问题标题】:(Nodejs Nock) Is there a cleaner way to simulate a response that changes with time in Nock(Nodejs Nock) 有没有更简洁的方法来模拟 Nock 中随时间变化的响应
【发布时间】:2020-06-26 15:58:16
【问题描述】:

我知道 nock 允许像我在这里一样为同一端点排队模拟:

nock('https://example.test')
.get('/')
.delayConnection(400)
.reply(200, MockApiResponses.proccessing) //response on first get
.get('/')
.delayConnection(150)
.reply(200, MockApiResponses.goodRetrieve) //response on second

我希望模拟一个场景,其中我的第一个回复在前 5-20 分钟内发送,然后是最后一个。但是,我不想将这些获取、延迟和回复中的 100 个链接在一起。

【问题讨论】:

    标签: node.js nock


    【解决方案1】:

    你有几个不同的选择。

    首先,如果你的延迟对于前N个请求是一致的,你可以使用.times()方法为你的第一个请求指定一个大的数字。

    如果这对您的情况不起作用,您始终可以使用动态响应函数并编写您自己的逻辑来返回哪个响应主体。 Docs for specifying responses.

    nock('https://example.test')
      .get('/')
      .reply(200, () => {
        if (someLogic()) return MockApiResponses.proccessing;
        return MockApiResponses.goodRetrieve;
      })
    

    【讨论】:

      猜你喜欢
      • 2013-06-01
      • 2017-04-15
      • 2015-08-05
      • 2019-12-20
      • 1970-01-01
      • 1970-01-01
      • 2017-07-24
      • 1970-01-01
      • 2020-04-01
      相关资源
      最近更新 更多