我正在对我的指令之一进行单元测试,但出现以下错误
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/