Attempting unit test in karma using AngularMock if my function returned a promise that was rejected, but might not seem surprising at all in this matter.
I have a service like UserService that has a function: processIdentityResponse that returns a promise that is either allowed or rejected depending on the logic inside:
processIdentityResponse: function(response) { var deferred = $q.defer(); if (response.data.banned) { deferred.reject(response); } else { deferred.resolve(response); } return deferred.promise; }
I want to check that if a forbidden property exists, the rejected promise is returned, and if not, then it is allowed ... how can I achieve this?
I tried something like the following without success:
it('should return a rejected promise if status is a string', function() { var rejected = false; UserService.processIdentityResponse(data).catch(function() { rejected = true; }); expect(rejected).toBe(true); });
source share