【问题标题】:change the response for single request among multiple request in nock在 nock 的多个请求中更改单个请求的响应
【发布时间】:2021-08-06 10:56:57
【问题描述】:

我使用 express 并尝试创建一个用于测试的 API 我使用了 nock,所以我发出了一个 get 请求,我不想更改其响应,之后我想做 put 请求并想更改该部分的响应但是当我尝试这样做时,我在第一个获取请求时收到错误,我不想更改。我收到错误Error: Nock: No match for request

  nock(baseURI + '/' + imp.http.relativeURI , { 'encodedQueryParams': true })
            .put('/' +imp.file.fileName)
            .reply(422,
              '<response><errormessage>some error message</errormessage></response>',
              [ 'Content-Type', 'application/xml; charset=UTF-8' ])

【问题讨论】:

    标签: node.js express nock


    【解决方案1】:

    模拟第二个请求时,您需要使用allowUnmocked 标志。 https://github.com/nock/nock#allow-unmocked-requests-on-a-mocked-hostname

    nock(baseURI + '/' + imp.http.relativeURI , { allowUnmocked: true, 'encodedQueryParams': true })
            .put('/' +imp.file.fileName)
            .reply(422,
              '<response><errormessage>some error message</errormessage></response>',
              [ 'Content-Type', 'application/xml; charset=UTF-8' ])
    

    这个位:nock(baseURI + '/' + imp.http.relativeURI) 基本上是在告诉 Nock 你正在测试这个端点,默认情况下它假设你只期望显式地模拟到那个端点的调用。 allowUnmocked 推翻了这一假设,并允许实时呼叫正常进行,除非您明确将其模拟出来。

    【讨论】:

      猜你喜欢
      • 2021-03-27
      • 2016-02-21
      • 2012-08-04
      • 2018-03-26
      • 1970-01-01
      • 2021-07-17
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多