单元测试使用 Karma 和 Jasmine 调用控制器的功能

Unit testing call a function of a controller with Karma and Jasmine

这是我的 angular 控制器 :-

    angular.module('authoring-controllers', []).
        controller('NavCtrl', function($scope, $location, BasketNavigationService) {

$scope.test= function() {
                $scope.testVar = BasketNavigationService.showBasketList();
            };
        });

测试 class

describe('NavCtrl', function() {
    var scope, $location, createController;

    beforeEach(inject(function ($rootScope, $controller, _$location_) {
        $location = _$location_;
        scope = $rootScope.$new();

        createController = function() {
            return $controller('NavCtrl', {
                '$scope': scope
            });
        };
    }));

    it('should create $scope.testVar when calling test', 
        function() {
          expect(scope.testVar).toBeUndefined();
          scope.test();
          expect(scope.testVar).toBedefined();
      });
});

当我 运行 测试用例 :- scope.test() 未定义时出现错误..

如果我从控制器中删除了 BasketNavigationService 功能,那么它可以正常工作..

请帮我解决那个 karma 测试用例。

$scope.showBasketList 不是可以用 $scope.showBasketList() 调用的函数。它是一个等于 BasketNavigationService.showBasketList() 的 return 值的变量。

如果您想引用该函数,它应该在您的控制器中 $scope.showBasketList = BasketNavigationService.showBasketList;

这是工作演示,希望对您有所帮助。问题在于注入依赖项。

//--- 代码 --------------------------

(function(angular) {
  // Create module
  var myApp = angular.module('myApp', []);

  // Controller which counts changes to its "name" member
  myApp.controller('MyCtrl', ['$scope', 'BasketNavigationService',
    function($scope, BasketNavigationService) {

      $scope.test = function() {
        $scope.testVar = BasketNavigationService.showBasketList();;
      };
    }
  ]);


})(angular);

// ---规格------------------------

describe('myApp', function() {
  var scope,
    controller;
  beforeEach(function() {
    module('myApp');
  });

  describe('MyCtrl', function() {
    beforeEach(inject(function($rootScope, $controller) {
      scope = $rootScope.$new();
      controller = $controller('MyCtrl', {
        '$scope': scope,
        'BasketNavigationService': {
          showBasketList: function() {
            return null;
          }
        }
      });
    }));
    it('should create $scope.testVar when calling test',
      function() {
        expect(scope.testVar).toBeUndefined();
        scope.test();
        //     scope.$digest();
        expect(scope.testVar).toBeDefined();
      });
  });


});

// --- 亚军------------------------

(function() {
  var jasmineEnv = jasmine.getEnv();
  jasmineEnv.updateInterval = 1000;

  var htmlReporter = new jasmine.HtmlReporter();

  jasmineEnv.addReporter(htmlReporter);

  jasmineEnv.specFilter = function(spec) {
    return htmlReporter.specFilter(spec);
  };

  var currentWindowOnload = window.onload;

  window.onload = function() {
    if (currentWindowOnload) {
      currentWindowOnload();
    }
    execJasmine();
  };

  function execJasmine() {
    jasmineEnv.execute();
  }

})();

<script src="http://jasmine.github.io/1.3/lib/jasmine.js"></script>
<script src="http://jasmine.github.io/1.3/lib/jasmine-html.js"></script>
<script src="https://code.angularjs.org/1.2.9/angular.js"></script>
<script src="https://code.angularjs.org/1.2.9/angular-mocks.js"></script>

<link href="http://jasmine.github.io/1.3/lib/jasmine.css" rel="stylesheet" />

fiddle : http://jsfiddle.net/invincibleJai/pf1deoom/1/