我在$ timeout内有一段代码执行一个promise。 当我编写单元测试时,我使用$ timeout.flush(),我可以进入$ timeout块。然后解析承诺(因为$ timeout在内部调用$ scope。$ apply())但是在方法代码完成并且冒泡到jasmine后我得到错误TypeError:无法读取未定义的属性'$$ nextSibling'。 其他人以前经历过这个吗?
控制器代码:
$scope.timeoutFunction = function(){
$timeout(function () {
$scope.callFunction().then(function () {
callAnotherFunction();
});
}, 0);
}
单元测试代码:
$scope.timeoutFunction();
scope.$digest(); // not needed
$timeout.flush(); // even tried $timeout.flush(0);
此外,如果我模拟超时是一个回调函数,它的工作原理。 例如:
function mockedTimeout() {
arguments[0]();
}
$timeout = mockedTimeout;
然后我们只使用$ scope。$ digest来调用promise,超时立即运行。它可能是角虫吗?
完成错误堆栈:
TypeError: Cannot read property '$$nextSibling' of undefined
at n.$get.n.$digest (http://localhost:51679/referenceFile?path=D:\development\Projects\WebSite\Scripts\Libs\Angular\angular.min.js:124:101)
at n.$get.n.$apply (http://localhost:51679/referenceFile?path=D:\development\Projects\WebSite\Scripts\Libs\Angular\angular.min.js:126:293)
at Object.fn (http://localhost:51679/referenceFile?path=D:\development\Projects\WebSite\Scripts\Libs\Angular\angular.min.js:139:3)
at Function.angular.mock.$Browser.self.defer.flush (http://localhost:51679/referenceFile?path=D:\development\Projects\WebSite\Scripts\TDD\angular-mocks.js:127:42)
at Function.angular.mock.$TimeoutDecorator.$delegate.flush (http://localhost:51679/referenceFile?path=D:\development\Projects\WebSite\Scripts\TDD\angular-mocks.js:1732:28)
at null.<anonymous> (http://localhost:51679/Tests.js:254:18)
at jasmine.Block.execute (http://localhost:51679/referenceFile?path=D:\development\Projects\WebSite\Scripts\jasmine.js:1064:17)
at jasmine.Queue.next_ (http://localhost:51679/referenceFile?path=D:\development\Projects\WebSite\Scripts\jasmine.js:2096:31)
at jasmine.Queue.start (http://localhost:51679/referenceFile?path=D:\development\Projects\WebSite\Scripts\jasmine.js:2049:8)
at jasmine.Spec.execute (http://localhost:51679/referenceFile?path=D:\development\Projects\WebSite\Scripts\jasmine.js:2376:14)"