【问题标题】:Angular-Bootstrap "Possibly unhandled rejection: undefined thrown"Angular-Bootstrap“可能未处理的拒绝:未定义的抛出”
【发布时间】:2017-02-06 15:59:25
【问题描述】:

当我遇到这个奇怪的问题时,我正在编写测试代码。如果我在执行myModal.dismiss(); 之后执行$rootScope.$digest();,我会得到Possibly unhandled rejection: undefined thrown。我查看了 angular-bootstrap 库中 Modal 的单元测试,我注意到他们正在使用 $animate 做一些事情。也许这有关系?

it('tests weird failure', function(){
  var myModal = this.$uibModal.open({
    template: '<div class="modalTwo"></div>',
    size: "md"
  });

  this.$rootScope.$digest();
  myModal.dismiss();
  this.$rootScope.$digest();
});

【问题讨论】:

    标签: angularjs angular-bootstrap


    【解决方案1】:

    我不确定这是否有帮助,但可能会。我进行了涉及 $uibModal 被解雇的工作测试,但是当我在一台新机器上为我的项目运行 npm install 时,我开始看到其中一个错误。我的问题最终是我没有在我的测试代码中处理被拒绝的承诺的情况。奇怪的是,测试仍然在我的旧机器上运行,所以我做了一些挖掘(当然不是很多),发现两台机器(1.0.2 和 1.1.0)的 karma-jasmine 版本不同.我相信这就是我在一台机器上而不是另一台机器上得到错误的原因。

    在测试中重现问题的代码:

    $confirm({
        text: 'Are you sure you want to do this?',
        title: 'Are you sure?'
    }).then(
        function() {
            $scope.model.things.pop();
        }
    );
    

    测试此代码是否被拒绝的承诺的规范:

    scope.model.things = [{id: 1}, {id: 2}, {id: 3}];
    
    deferred.reject();
    
    expect(scope.model.things).toEqual([{id: 1}, {id: 2}]);
    

    请注意,在被测代码中,如果 $confirm(来自 angular-confirm 库的东西)被拒绝(在这种情况下,这意味着用户在弹出窗口中单击“否”),将不会调用第二个函数。对我的代码进行这个小改动让我的测试通过:

    $confirm({
        text: 'Are you sure you want to do this?',
        title: 'Are you sure?'
    }).then(
        function() {
            $scope.model.things.pop();
        }, function() {}
    );
    

    这里唯一的区别是我有一个空函数来“处理”被拒绝的承诺。我希望这会有所帮助。

    【讨论】:

    • 我遇到了一些其他错误,最终发现我也将 Angular 从 1.5.x 更新到了 1.6.x。这解决了我遇到的另一个问题,我认为这也可能与它有关。
    猜你喜欢
    • 2017-05-07
    • 1970-01-01
    • 1970-01-01
    • 2017-09-21
    • 2022-01-03
    • 2023-04-06
    • 1970-01-01
    • 2017-04-25
    • 1970-01-01
    相关资源
    最近更新 更多