如何通过Nock对象获取请求的名称

时间:2017-05-26 15:23:30

标签: nock

在我的测试代码中,我检查断言已经调用了所有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对象获取有关请求的有用/识别信息?

1 个答案:

答案 0 :(得分:2)

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

nock.pendingMocks()