Javascript 茉莉花-测试承诺。如何在回调函数中测试函数?
我有(Angular.js 1.5+ng redux)代码: 或咖啡脚本:Javascript 茉莉花-测试承诺。如何在回调函数中测试函数?,javascript,angularjs,unit-testing,coffeescript,jasmine,Javascript,Angularjs,Unit Testing,Coffeescript,Jasmine,我有(Angular.js 1.5+ng redux)代码: 或咖啡脚本: update: (itemId, value) -> (dispatch, getState) -> params = { id: itemId, action: value } resultsAPI.change(params).then(results) -> dispatch({ type: "CHANGE_STATE" itemId: itemId
update: (itemId, value) -> (dispatch, getState) ->
params = {
id: itemId,
action: value
}
resultsAPI.change(params).then(results) ->
dispatch({
type: "CHANGE_STATE"
itemId: itemId
output: results
})
我想写一个测试,看看是否已经向API发出了请求具有正确的参数。
我还要检查是否调用了after.then()分派函数。有什么想法吗?在测试中,您必须发送API请求,然后进行测试。然后,您可以使用
来调用()功能。我建议大家看看这篇文章:GoogleJasmine异步测试。有很多东西。请在发布到Stack overflow之前进行研究,如果文档中有您不理解的内容,请将其包含在问题中。在测试中,您必须发送API请求,然后对其进行测试。然后,您可以使用来调用()功能。我建议大家看看这篇文章:GoogleJasmine异步测试。有很多东西。请在发布到Stack overflow之前进行研究,如果文档中有您不理解的内容,请将其包含在问题中。
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()
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()