【发布时间】:2015-03-05 13:34:40
【问题描述】:
我正在对我的一个指令进行单元测试,但出现以下错误
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() 给了我这个错误。
所以问题是,测试这种情况的首选方法是什么?
【问题讨论】:
标签: angularjs unit-testing angularjs-directive jasmine