Attempting to test an async function using Karma and Jasmine in an AngularJS v1.5.0 project. This async function, labeled as (a), contains 2 await statements:
async function a(){
let var1 = await b()
console.log(var1)
let var2 = await c()
}
function b(){
return $http.get(url1).then(
function(response){
...
}
)
}
function c(){
return $http.get(url2).then(
function(response){
...
}
)
}
The testing scenario is as follows:
it('testA', inject(async function($httpBackend) {
$httpBackend.when('GET','url1').respond(200, [myresult])
$httpBackend.when('GET','url2').respond(200, [myresult])
MyController().a()
$httpBackend.flush();
}));
The issue arises when calling $httpBackend.flush();
. It seems to only 'flush' the first await statement, causing the execution of function 'a' to halt before reaching console.log(var1)
. Assistance in resolving this problem would be greatly appreciated.
It is important to note that modifications to the actual code are not allowed, the objective is solely to create a reliable test for it.
Attempts to use await a()
within the test have proven unsuccessful, as it does not even resolve the initial await statement.