I'm having hard times trying to test promise-based code in Angularjs.
I have the following code in my controller:
$scope.markAsDone = function(taskId) {
tasksService.removeAndGetNext(taskId).then(function(nextTask) {
goTo(nextTask);
})
};
function goTo(nextTask) {
$location.path(...);
}
I'd like to unit-test the following cases:
- when
markAsDone
is called it should calltasksService.removeAndGetNext
- when
tasksService.removeAndGetNext
is done it should change location (invokegoTo
)
It seems to me that there is no easy way to test those two cases separately.
What I did to test the first one was:
var noopPromise= {then: function() {}}
spyOn(tasksService, 'removeAndGetNext').andReturn(noopPromise);
Now to test the second case I need to create another fake promise that would be always resolved
. It's all quite tedious and it's a lot of boilerplate code.
Is there any other way to test such things? Or does my design smell?