2017-05-26 66 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' ]