[英]Jasmine - testing promises. How to test function in callback?
我有(Angular.js 1.5 + ng-redux)代碼:
update: function(itemId, value) {
return function(dispatch, getState) {
var params;
params = {
id: itemId,
action: value
};
resultsAPI.change(params).then(function(results) {
dispatch({
type: "CHANGE_STETE",
itemId: itemId,
output: results
});
});
};
}
或coffeescript:
update: (itemId, value) -> (dispatch, getState) ->
params = {
id: itemId,
action: value
}
resultsAPI.change(params).then(results) ->
dispatch({
type: "CHANGE_STATE"
itemId: itemId
output: results
})
我想編寫一個測試,看看是否已使用正確的參數對API進行了請求。 我還會檢查是否調用.then()調度函數。 關於如何去做的任何想法?
it "call dispatch after .then", ->
filterId = 1
value = "stop"
params =
id: filterId
action: value
dispatch = jasmine.createSpy("dispatch")
dispatch = =>
type: "CHANGE_STATE"
filterId: 2
spyOn(@resultsAPI, "change").and.callFake(-> dispatch())
@ngRedux.dispatch(update())
@InvestigationsResultsAPI.changeState(params)
dispatch()
expect(dispatch).toHaveBeenCalled()
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.