I have an issue related to testing with nested promises.
I have this method M as below in a 'Service'
M(){
M1().then (
M2();
return promise1;
)
};
and M2 and M1 has similar definitions like
M2(){
var deferred = $q.defer();
// for M1 u may call someUrl1
$http.get(someUrl2).then(function (success) {
deferred.resolve(success.data);
}, function () {
deferred.reject();
});
return deferred.promise;
}
And here is the problem: I have a test case which does below
spyOn(Service, 'M2').and.callThrough();
httpBackend.expectGET(SomeUrl1).respond(200,Response1, {'Content-type': 'application/json'});
httpBackend.expectGET(SomeUrl2).respond(200,Response2, {'Content-type': 'application/json'});
Service.M().then(function (Response1) {
expect(Service.M2).toHaveBeenCalled();
// here goes some expect operations on response coming from M()
});
httpBackend.flush();
So as above If I call M() The promise of only M1 is getting resolved and not the M2 ( as its nested) any idea how to also resolve the nested promise as well.
cant use $rootscope.$digest() or scope.$apply() as this is at service level.
For now the test case gives: Method M2 has never been called and resolves the result from M2 as a promise object but not the actual response
FYI : The unit test on M2 /M1 alone works as has only one promise .
Please let me know how to resolve this.