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

时间:2015-02-02 09:56:42

标签: angularjs unit-testing karma-jasmine

这是我的角度控制器: -

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

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

TEST课程

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 功能,那么它正在运行..

请帮我解决业力测试案例。

2 个答案:

答案 0 :(得分:1)

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

如果你想引用那个函数,它应该是你控制器中的$scope.showBasketList = BasketNavigationService.showBasketList;

答案 1 :(得分:0)

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

// --- CODE --------------------------

(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);

// --- SPECS -------------------------

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


});

// --- Runner -------------------------

(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" />

小提琴:http://jsfiddle.net/invincibleJai/pf1deoom/1/