【问题标题】:How to get the name of a request via Nock object如何通过 Nock 对象获取请求的名称
【发布时间】:2017-10-27 13:21:50
【问题描述】:

在我的测试代码中,我检查断言是否已调用所有 nock,如果未调用 nock,我有一个半有用的错误消息要转储出来(因为默认错误消息是无用的):

try {
  assertions(data, result);
  if (assertNock !== null) {
    // Expect that all mocked calls were made
    if (nock.isDone() !== !!assertNock) {
      console.error('One or more of your Nock matchers was never called.');
    }
    expect(nock.isDone()).toBe(!!assertNock);
  }
  done();
} catch (err) {
  ...
}

但是,我希望能够指定 哪个 调用未进行。但是,我似乎找不到从nock 对象获取该信息的方法,如下所示:

{ [Function: startScope]
  emitter:
   EventEmitter {
     domain: null,
     _events: {},
     _eventsCount: 0,
     _maxListeners: undefined },
  define: [Function: define],
  loadDefs: [Function: loadDefs],
  load: [Function: load],
  enableNetConnect: [Function: enableNetConnect],
  disableNetConnect: [Function: disableNetConnect],
  removeInterceptor: [Function: removeInterceptor],
  activeMocks: [Function: activeMocks],
  pendingMocks: [Function: pendingMocks],
  isDone: [Function: isDone],
  isActive: [Function: isActive],
  activate: [Function: activate],
  cleanAll: [Function: cleanAll],
  recorder:
   { rec: [Function: record],
     clear: [Function: clear],
     play: [Function] },
  back: { [Function: Back] setMode: [Function], fixtures: null, currentMode: 'dryrun' },
  restore: [Function: restore]
}

我如何才能获得关于不是从 nock 对象发出的请求的有用/识别信息?

【问题讨论】:

    标签: nock


    【解决方案1】:

    根据documentation,拦截器一旦被调用就会被移除。有了这些知识,就可以使用nock.activeMocks(),它将返回一组仍处于活动状态的项目。如果您已将 .persist() 添加到任何 nocks,它们仍将在列表中。在这种情况下,您可能需要使用 nock.pendingMocks(),它只会返回尚未调用的 nocks。

        nock.activeMocks(); // [ 'GET https://www.example.com:443/fake/url', 'POST https://sts.amazonaws.com:443/' ]
        nock.pendingMocks(); // [ 'GET https://www.example.com:443/fake/url' ]
    

    【讨论】:

      猜你喜欢
      • 2015-06-06
      • 1970-01-01
      • 2011-04-27
      • 1970-01-01
      • 2013-01-02
      • 2014-09-23
      • 1970-01-01
      • 1970-01-01
      • 2021-02-21
      相关资源
      最近更新 更多