在我的测试代码中,当我检查断言所有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对象发出的请求的有用/标识信息?
发布于 2018-02-28 15:48:22
根据文档,一旦调用了拦截器,就会删除它们。有了这些知识,您就可以使用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' ]https://stackoverflow.com/questions/44205118
复制相似问题