2017-07-24 88 views
0

我要測試的這款終極版中間件代碼中間件我反應過來的應用程序:測試終極版與承諾

export const dispatchActionFromPromise = next => (promise, name) => { 
     promise.then(data => { 
     next({ 
      type: `${name}_RECEIVED`, 
      data 
     }); 
     }, 
     error => { 
     return next({ 
      type: `${name}_ERROR`, 
      error 
     }); 
     }); 
}; 

const dataService = store => next => action => { 
    next(action); 

    switch (action.type) { 
    case GET_ORGANISATIONS: 
     dispatchActionFromPromise(next)(ApiClient.getOrganisations(), GET_ORGANISATIONS); 
     break; 
    default: 
     break; 
    } 

}; 

export default dataService; 

我想測試dispatchActionFromPromise()這需要一個承諾(ApiClient.getOrganisations()返回一個承諾)。

什麼是值得在這裏測試,如果有的話,什麼是最好的方法來測試這個?

我正在使用摩卡和柴進行測試。

回答

0

我想測試getOrganisations()返回一個承諾

隨着UNITEST,你只是測試只有一個功能。在你的情況下,你想測試的功能是getOrganisations()。那麼爲什麼我們關心調用這個函數的上下文呢?

如果你想寫摩卡測試一個函數,在reactjs和redux中返回promise,你可以編寫模擬函數來返回假結果。但還有另一個有趣的方式,我建議你使用的是使用REDX傳奇,因爲它使測試更容易。閱讀辦公室紀錄傳奇here欲知更多信息。