I use $resource to configure some API calls, and during testing, I took a general approach to injecting $q and then executed
mockMyService.doSomethingAsync.andReturnValue($q.when(successResponse))
This works very well, however I have a method that looks like this:
and unfortunately, the mocking strategy described above does not seem to work when the $promise.then chain is completed at the end. As a result, I get the following error:
TypeError: 'undefined' is not an object (evaluating 'MyService.doSomethingAsync(params).$promise.then')
Methods that simply end in doSomethingAsync().$promise pass the tests without problems using this mocking strategy.
(Additional info: these are Jasmine tests that run using Karma and PhantomJS)
source share