0

在我的测试代码中,我检查断言所有 nocks 已被调用,如果没有调用 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 对象发出的请求的有用/识别信息?

4

1 回答 1

2

根据文档,拦截器一旦被调用就会被删除。有了这些知识,就可以使用nock.activeMocks()which 将返回一组仍处于活动状态的项目。如果您已添加.persist()到任何 nocks,它们仍将在列表中。在这种情况下,您可能希望使用nock.pendingMocks()which 只会返回尚未调用的 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' ]
于 2018-02-28T15:48:22.157 回答