mardi 6 septembre 2016

Angular controller unit tests with mock service not invoking either success or error callbacks

I have below controller and its respective service code. In my spec when I call ctrl.loadProjects() function I am assigning some value to self.info2 variable both in success and error callbacks but none of them are executing. Can any one let me know where I am going wrong. I have similar kind of other controller, service and controllerSpec which are working fine but this one is neither giving any error nor working as expected. Here are the code snippets. In my spec Test Case0 is passing but 1 and 2 are failing.

Controller

 (function () {
    'use strict';
    angular.module('app.home1', ['usher.projectapi'])
        .component('appHome1', {
            templateUrl : 'views/home1.html',
            controller  : MyTestCtrl
        });
    /* @ngInject */
    function MyTestCtrl(projectapiSvc) {
        var self = this;
        self.info1 = '';
        self.info2 = '';
        self.loadProjects = loadProjects;

        function loadProjects() {
            projectapiSvc.getProjects().then(successCallback,errorCallback);
            self.info1 = 'Executed getprojects function';
            function successCallback(data) { self.info2="Response received"; }
            function errorCallback(error) { self.info2="Error occurred"; }
        }
    }
})();

Service

angular.module('projectSvcModule')
 .factory('projectService', function($resource,projectResourceService) {
    var projectService= { getProjects :getProjects}
    function getProjects() {
        return projectResourceService.getProjects()
        .$promise.then(successCallback, failCallback);
            function successCallback(data) {
            return data;
            }
        function failCallback() {
     // console.log('Failed');
            }
      }
});

Resource Service

angular.module('projectSvcModule',['ngResource'])
.factory('projectResourceService', function($resource) {
return $resource('/api/projects', null, {
  getProjects: {
    method: 'GET' 
    }
  });
});

Controller Spec

  describe('Controller: MyTestCtrl',function () {

  var ctrl;
  var deferred;
  var $rootScope;
  var $timeout;
  var projectService;
  beforeEach(function () {
      angular.mock.module('app.home1') ;
      angular.mock.inject(function (_$q_, _$rootScope_, _$timeout_, _projectService_,_$componentController_) {
          ctrl = $componentController('appHome1');
          $rootScope = _$rootScope_;
          projectService = _projectService_;
          $timeout = _$timeout_;
          deferred = _$q_.defer();
          spyOn(projectService, 'getProjects').and.returnValue(deferred.promise);
      });
  });

    //this test case is passed
     it('Test Case0: Should call loadProjects function ',function () {
           ctrl.loadProjects();
                expect(ctrl.info1).toBe('Executed getprojects function');
      });

    //this test case is failing
     it('Test Case1: Should call success callback ',function () {
        ctrl.loadProjects();
        deferred.resolve([{name : 'xyz' , id : 1}]);
        $timeout(function () { $rootScope.$apply(); });
        expect(ctrl.info2).toBe('Response received');
      });
      //this test case too failing
      it('Test Case2: Should call error callback ',function () {
      ctrl.loadProjects();
      deferred.reject();
      $timeout(function () { $rootScope.$apply(); });
      expect(ctrl.info2).toBe('Error occurred');
      });
  });

Aucun commentaire:

Enregistrer un commentaire