【问题标题】:Error: Nock: No match for request错误:Nock:与请求不匹配
【发布时间】:2018-07-18 09:14:10
【问题描述】:

我收到以下错误

{ error: 
   { Error: Nock: No match for request {
     "method": "GET",
     "url": "http://localhost:3000/admin/orders/30075889/transactions.json",
     "headers": {
       "content-type": "application/json",
       "host": "localhost:3000"
     }
   } Got instead {
     "method": "GET",
     "url": "http://localhost:3000/admin/orders/30075889/transactions.json",
     "headers": {
       "content-type": "application/json",
       "host": "localhost:3000"
     }
   }

url和预期一样,不知道哪里错了,有指针吗?

【问题讨论】:

  • 我正在调用 2 个 api,1 个是 post,1 个是 get。并做 Promise.all([1stApi, 2edApi])。但得到错误 - 错误:诺克:请求不匹配。当我将两者中的任何 1 放入 Promise.all() 中时,就可以正常工作了。有什么建议为什么会在 2 次 api 调用中发生这种情况?

标签: node.js nock


【解决方案1】:

有时可能是encodedQueryParams: true 选项的问题

编码查询参数是这些天的默认行为,没有这样的选项也能很好地工作

【讨论】:

    【解决方案2】:

    你可以随时显示准备好的箭尾,它总是一个错字... ;)

    console.log(nock.activeMocks());

    【讨论】:

      【解决方案3】:

      使用.log(console.log) 查看确切的错误消息。

      前:

      nock('https://test.org/sample')
      .persist()
      .log(console.log)
      .get('/test')
      .query({})
      .reply(200, response);
      

      当你使用它并运行测试时,你会在控制台中看到类似这样的东西

      matching https://test.org/sample/test to GET https://test.org/sample/test with query({}): **true/false**.
      

      如果它说的是真的,那么你的请求应该是好的。但如果它显示为 false,请检查两个请求并确保它们匹配。

      【讨论】:

      • 它被弃用了吗? TypeError: nock(...).log is not a function
      • 使用环境变量DEBUG=nock.*而不是.log(...),不再支持日志。即:DEBUG=nock.* jest -i your/test/file.test.js
      【解决方案4】:

      默认情况下,Nock 拦截器不会持续存在。对于每个请求 nock 都需要一个拦截器。看起来您只设置了一次拦截器并期望它适用于每个请求。如果您希望拦截器持续使用.persist() 选项,如下所示。

      var scope = nock('http://localhost.com')
        .persist()
        .get(/.*/)
        .reply(200, 'Nock all get requests!');
      

      【讨论】:

      • I hace persist() 但错误仍然出现......它刚刚开始出现
      猜你喜欢
      • 2021-02-03
      • 2019-07-19
      • 2020-08-09
      • 2019-09-19
      • 2015-11-21
      • 2019-07-23
      • 2016-04-05
      • 2021-01-25
      • 1970-01-01
      相关资源
      最近更新 更多