当我遇到这个奇怪的问题时,我正在写测试代码。如果我在执行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();
});发布于 2017-02-13 10:49:11
我不确定这是否有帮助,但它可能会。我有一些工作测试涉及$uibModal被关闭,但是当我在一台新机器上为我的项目运行npm install时,我开始看到其中一台机器出现了这个错误。我的问题最终是我没有处理被测试的代码中被拒绝的承诺的情况。奇怪的是,测试仍然在我的旧机器上运行,所以我做了一些挖掘(诚然不是很多),发现karma-jasmine的版本在两台机器上是不同的(1.0.2和1.1.0)。我相信这就是为什么我在一台机器上得到错误,而在另一台机器上没有。
用于重现该问题的测试代码:
$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库的内容)被拒绝的情况下被调用(在本例中,这意味着用户在弹出窗口中单击了no )。对我的代码进行这个小小的更改,就可以让我的测试通过:
$confirm({
text: 'Are you sure you want to do this?',
title: 'Are you sure?'
}).then(
function() {
$scope.model.things.pop();
}, function() {}
);这里唯一的区别是我有一个空函数来“处理”被拒绝的承诺。我希望这能在某种程度上有所帮助。
https://stackoverflow.com/questions/42072030
复制相似问题