Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/angularjs/25.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/powershell/13.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 茉莉花-测试承诺。如何在回调函数中测试函数?_Javascript_Angularjs_Unit Testing_Coffeescript_Jasmine - Fatal编程技术网

Javascript 茉莉花-测试承诺。如何在回调函数中测试函数?

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

我有(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
    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()