I'm trying to test my AngularJS controller with Jasmine, using Karma. But a $timeout
which works well in real-life, crashes my tests.
Controller:
var Ctrl = function($scope, $timeout) {
$scope.doStuff = function() {
$timeout(function() {
$scope.stuffDone = true;
}, 250);
};
};
Jasmine it block (where $scope
and controller have been properly initialized):
it('should do stuff', function() {
runs(function() {
$scope.doStuff();
});
waitsFor(function() {
return $scope.stuffDone;
}, 'Stuff should be done', 750);
runs(function() {
expect($scope.stuffDone).toBeTruthy();
});
});
When I run my app in browser, $timeout
function will be executed and $scope.stuffDone
will be true. But in my tests, $timeout
does nothing, the function is never executed and Jasmine reports error after timing out 750 ms. What could possibly be wrong here?
question from:
https://stackoverflow.com/questions/17350492/angularjs-timeout-function-not-executing-in-my-jasmine-specs 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…