Jea*_*eri 5 unit-testing jasmine angularjs angularjs-directive
我正在对指令之一进行单元测试,但出现以下错误
Error: No deferred tasks to be flushed
Run Code Online (Sandbox Code Playgroud)
我需要测试的代码在$ timeout内部
$timeout(function () {
$window.doStuff();
});
Run Code Online (Sandbox Code Playgroud)
所以当我想检查是否doStuff有人打电话时
$timeout.flush();
expect($window.doStuff).toHaveBeenCalled();
Run Code Online (Sandbox Code Playgroud)
但是,现在我需要测试一个不会doStuff被调用的情况。所以我做了:
$timeout.flush();
expect($window.doStuff).not.toHaveBeenCalled(); // Notice the not here!!
Run Code Online (Sandbox Code Playgroud)
但是,由于$timeout没有被调用,$timeout.flush()给了我这个错误。
所以问题是,测试这种情况的首选方法是什么?