mercredi 30 septembre 2020

AngularJS - Jasmine - Cannot test Service in Controller callback function

I need help with Jasmine test. I have this controller with a simple service:

angular.module('${project.name}')
  .controller('MyControllerCtrl', function ($scope, InvocationService) {
    $scope.vm = {};
    var vm = $scope.vm; 
    vm.idResponse="";
    
    vm.invocacion_get = function(){
        var successCallback = function (response){
               vm.idResponse = response.data.totalElements;
               //More complex logic here that must tested, but no....
        }

        var errorCallback = function(error){
            vm.idResponse = error;
        }   
        
        var url = "prefernces/details?id=1";
        InvocationService.get(url, successCallback, errorCallback);
    } 
})

.factory('InvocationService', function($q) {
  var factory = {};           
  factory.get = function(url,successCallback, errorCallback) {
       var deferred = $q.defer();              
       var allData = $http.get("/risam-backend/rest/"+url).then(successCallback, errorCallback)
       deferred.resolve(allData);
       return deferred.promise;
     }
    return factory;
  });

The test code is (work fine):

describe('Testing a controller', function() {
      var $scope, ctrl;
      var serviceMock;
      
      beforeEach(function (){
        serviceMock = jasmine.createSpyObj('InvocationService', ['get']);
        module('${project.name}');          

        inject(function($rootScope, $controller, $q, _$timeout_) {
          $scope = $rootScope.$new();
          ctrl = $controller('MyControllerCtrl', {
            $scope: $scope,
            InvocationService : serviceMock        
          });
        });
      });

      it('test method vm.invocacion_get()', function (){
        $scope.vm.invocacion_get();
        expect(serviceMock.get).toHaveBeenCalled(); //This work ok
      });
    });

Whit this test only can check that metod InvocationService.get is called, it's ok.

But I need test the logic of functions 'successCallback' and 'errorCallback' (See vm.invocacion_get() function). This functions is called after 'InvocationService.get' is invoqued, but i this test cannot see anything change. It does not matter if the data returned by the service is simulated; the idea is that the variable 'vm.idResponse' changes value.

Searching the web I saw some examples but couldn't get them to work, sorry.

Thanks!

Aucun commentaire:

Enregistrer un commentaire