angularjs - Angular/ Jasmine : Error: No deferred tasks to be flushed

标签 angularjs unit-testing angularjs-directive jasmine

我正在对我的指令之一进行单元测试,但出现以下错误

Error: No deferred tasks to be flushed

我需要测试的代码在 $timeout 内
$timeout(function () {
    $window.doStuff();
});

所以当我想检查是否 doStuff被称为我做
$timeout.flush();
expect($window.doStuff).toHaveBeenCalled();

但是,现在我需要测试一种我不期望的情况 doStuff被称为。所以我做了:
$timeout.flush();
expect($window.doStuff).not.toHaveBeenCalled(); // Notice the not here!!

然而,因为$timeout没有被调用,$timeout.flush()给我这个错误。

所以问题是,测试这种情况的首选方法是什么?

最佳答案

使用 $timeout.verifyNoPendingTasks()检查是否没有添加任何任务。

检查 Angular 文档 here :

Verifies that there are no pending tasks that need to be flushed.

关于angularjs - Angular/ Jasmine : Error: No deferred tasks to be flushed,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28879080/

相关文章:

javascript - 错误 : undefined is not an object (evaluating '$scope.inputcode.password' )

c# - 根据上下文为抽象属性创建具体类型

unit-testing - 测试驱动设计如何帮助一个人的软件项目?

unit-testing - 如何测试枚举类型?

javascript - Datepicker 不更新 ng-model

javascript - 如何打开 ionic 列表中的特定 ionic 项目

javascript - 使用 AngularJs + Materialize.css 的多个下拉菜单

html - HTML 属性中的 AngularJS 条件

javascript - 使用 bindToController 时,指令的范围变量在构造函数中未定义

javascript - 从数组指令中删除重复项