【问题标题】:Jasmine error '$digest already in progress'Jasmine 错误'$digest 已经在进行中'
【发布时间】:2015-12-31 17:26:42
【问题描述】:

我用 IBM Mobilefirst 平台(ionic、AngularJS)开发了一个应用程序,现在我想用 Jasmine 框架测试这个应用程序。

以下代码是我要测试的控制器的一部分:

$scope.method= function(...) {
    ServiceSvc.insertProduct().then(function() {
        $scope.finishsavingAdding = true;
        $scope.finishsavingDeleting = false;

        ServiceSvc.getAllProducts().then(function(data) {
            $scope.product= data.invocationResult.resultSet;
            $scope.$broadcast('scroll.refreshComplete'); 
            $scope.$apply(); }) })}

我尝试使用以下代码进行单元测试,但它抛出了这个错误:

错误:[$rootScope:inprog] $digest 已经在进行中

it("should call method", function(){
    $scope.method();
    expect(ServiceSvcMock.insertProduct).toHaveBeenCalled();

    var data = {invocationResult: {resultSet:"test"}};
    deferred.resolve(data);          
    $scope.$apply();

    expect($scope.finishsavingDeleting).toBe(false);    
    expect(ServiceSvcMock.getAllProducts).toHaveBeenCalled();   
});

我已经阅读了 Stack Overflow 上的其他主题,但它们对我没有帮助。 我该如何解决这个问题?

【问题讨论】:

  • $scope.$apply(); 如果getAllProducts() 使用的是$q/$http/$resource,则不需要

标签: angularjs unit-testing jasmine


【解决方案1】:

使用$scope.$apply(); 时会引发错误。这是因为,您的那段代码(包含$scope.$apply())已经在 Angular 的摘要循环中。

删除$scope.$apply(),它应该可以正常工作。

【讨论】:

  • 感谢您的快速回答,但是当我从测试中删除应用行时,finishsavingDeleting 将未定义并且不会调用 ServiceSvcMock.getAllProducts。您还有其他建议吗?
  • 从 $scope.method 中删除该行
  • 谢谢你成功了 :) 目前我不能支持你的评论,但是当我有更多声誉时我会这样做
  • @JavaAndroider 如果您认为正确,请接受答案。 :)
  • 你不能投票,但你可以接受它作为正确的答案。
猜你喜欢
  • 2013-01-28
  • 2014-10-01
  • 1970-01-01
  • 1970-01-01
  • 2015-07-26
  • 2014-08-12
  • 1970-01-01
  • 1970-01-01
  • 2014-03-18
相关资源
最近更新 更多