角度控制器单元使用模拟服务进行测试,不会调用成功或错误回调

时间:2016-09-06 18:41:47

标签: angularjs unit-testing testing controller

我有以下控制器及其各自的服务代码。在我的规范中,当我调用ctrl.loadProjects()函数时,我在self.info2变量中为成功和错误回调分配了一些值,但它们都没有执行。谁能让我知道我哪里出错了。我有类似的其他控制器,服务和controllerSpec工作正常,但这个既没有给出任何错误也没有按预期工作。以下是代码段。在我的规范中,测试Case0正在通过,但是1和2都失败了。

控制器

 (function () {
    'use strict';
    angular.module('app.home1', ['projectSvcModule'])
        .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"; }
        }
    }
})();

服务

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');
            }
      }
});

资源服务

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

控制器规格

  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');
      });
  });

0 个答案:

没有答案