2017-05-26 79 views
0

在我的测试代码中检查声明所有的nocks已被调用的时候,如果没有调用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] 
} 

我怎样才能得到有用的有关was't从箭扣提出的请求/识别信息目的?

回答

2

根据documentation,拦截器一旦被调用就被删除。有了这些知识,就可以使用nock.activeMocks()这将返回一个仍然有效的项目数组。如果您已将.persist()添加到任何一个诺克斯,他们仍将在列表中。在这种情况下,您可能想要使用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' ]