How to taunt AngularJS $ resource with $ prom.then in jasmine specs

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:

 # MyService MyService.doSomethingAsync(params).$promise.then -> $scope.isLoading = false # MyService Spec mockMyService = doSomethingAsync: jasmine.createSpy('doSomethingAsync') it 'calls service #doSomethingAsync', -> inject ($q) -> mockMyService.doSomethingAsync.and.returnValue($q.when(response)) controller.methodThatWrapsServiceCall() scope.$apply() expect(mockMyService.doSomethingAsync).toHaveBeenCalled() 

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)

+6
source share
1 answer

Actually, I got it!

I just changed my layout to return and.returnValue( { $promise: $q.when(response) } )

+6
source

Source: https://habr.com/ru/post/984195/


All Articles