During my Jasmine testing with angular promises, a question arose regarding timing. I came across a post at Unit-test promise-based code in Angular, but I still need some clarification on how it all functions. The concern is that since the then
method is handled asynchronously, there might be a risk of the expect
statement running before the value is actually assigned within the then
block. Or could it be that the digest cycle ensures that the value is assigned before the expect statement is executed, essentially acting as a blocking mechanism that ensures all promises are resolved before proceeding with the code.
function someService(){
var deferred = $q.defer();
deferred.resolve(myObj);
return deferred.promise;
}
it ('testing promise', function() {
var res;
var res2;
someService().then(function(obj){
res = "test";
});
someService().then(function(obj){
res2 = "test2";
});
$rootScope.$apply();
expect(res).toBe('test');
expect(res2).toBe('test2');
});