I already looked everywhere but could not find a solution yet for my particular case.
We are using angular 1.5 and a Karma/Jasmine setup for unit tests. In the initial source code, I used ES2017 async/await in the controller. That seemed to work fine as long as I added $apply of $digest manually at the end. So for example:
async function loadData() {
try {
vm.isLoading = true;
vm.data = await DataService.getData();
$scope.$apply();
}
catch (ex) {
vm.isLoading = false;
}
}
To write an automated test for this particular function, I tried to mock DataService.getData with Jasmine's spyOn
. So, I did something like this:
spyOn(DataService, 'getData').and.returnValue($q.when(fakeResult));
Adding the spy worked, but when running a test, the code seems to get struck and not resolve with fakeResult
. I tried adding $digest/$apply in the tests itself but could not fix it. I also did a lot of research, but still have no clue.
Does somebody have a clue?
Edit: testing the same method with $q
promises works fine, but I would really like to use async/await...