2016-08-03 53 views
2

我有一個actionCreator方法,我希望對其進行單元測試。它會進行一次firebase調用,如果成功或失敗,它會相對調度兩種方法。對涉及火力點調用的redux進行單元測試

看上去某事像這樣:

export function removeItem(itemId) { 
    return dispatch => { 
     return firebase.database().ref('items').child(itemId).remove() 
      .then(
       success => dispatch(updateList()), 
       error => dispatch(failRemovingItem(error)) 
      ); 
     }; 
} 

我下面從終極版的例子。 http://redux.js.org/docs/recipes/WritingTests.html

但我不能通過nock來模擬firebase調用。

有沒有人有類似單元測試的經驗?

回答

0

其中一個場景是提取行:

firebase.database().ref('items').child(itemId).remove() 

來幫手,例如FireBasehelper.removeEntity(ID),然後你可以很容易地通過以下常見的做法嘲笑行動的創建者這裏面幫手。然後,您的代碼應該是這樣的:

export function removeItem(itemId) { 
    return dispatch => { 
     return Databasehelper.removeEntity(itemId) 
      .then(
       success => dispatch(updateList()), 
       error => dispatch(failRemovingItem(error)) 
      ); 
     }; 
} 

在幫助你只回我們剛纔提取所以

function removeEntity(itemId) { 
    return firebase.database().ref('items').child(itemId).remove() 
} 
0

感謝您的答覆。實際上,我在Firebase電話周圍做了一名幫手。爲了簡單起見,我把它拿出來了。我遇到的麻煩是我不知道如何嘲笑這個動作。