Jasmine spyOn does not go to. Thats function with angular js

Here is my controller

function test(){
    console.log('trace1'); // this gets print on console
    myService.useService('someStuff')
      .then(function(data){
          console.log('trace2'); // this doesn't get print
      })
      .catch(function(error){
          console.log('trace3'); // this doesn't get print    
      });
}

Here is my test suite

describe('test controller promise', function () {

    beforeEach(inject(function(
        $controller,
        $q,
        _$rootScope_,
        $location,
        $anchorScroll,
        _myService_,
        $window
    ) {
        $rootScope = _$rootScope_;
        myService = _myService_;

        var testDeferred = $q.defer();
        testDeferred.reject('some_error_message');

        spyOn(myService, 'useService').and.callFake(function(){
            console.log('trace 5');  // this gets called
            return testDeferred.promise
        });

        myController = $controller('myController', {
            $rootScope: $rootScope,
            myService: myService,
            $location: $location,
            $anchorScroll: $anchorScroll,
            $window
        });

        $rootScope.$apply();
    }));

    it('test promise', function () {
        myController.test();

        // we see trace1 and trace5 gets called but not trace2 and trace3
});

I want the called .thenor .catchinvoked after calling the mock-service (ideally I need to see trace3for printing, as used reject), but we see that it is not called, as trace2and trace3will not be printed, but it is curious that Fake function is a challenge because how trace5is printed

I tried callFake, returnValueand using both rejectand resolve, all of them do not lead to a call .thenor .catch. What am I missing?

+4
source share

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


All Articles