【问题标题】:Call a controller function from Karma and Jasmine testing从 Karma 和 Jasmine 测试中调用控制器函数
【发布时间】:2015-02-02 11:42:20
【问题描述】:

这是我的角度控制器:-

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

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

测试类

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 功能,那么它就可以工作了..

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

【问题讨论】:

  • 您在expect(scope.testVar).toBedefined(); 中有错字,应该是expect(scope.testVar).toBeDefined();

标签: javascript angularjs unit-testing karma-jasmine


【解决方案1】:

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

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

【讨论】:

    【解决方案2】:

    请将您的createController 设置更改为:

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

    您没有注入所有依赖项。 我注入了假的BasketNavigationService,你可以注入真正的。

    【讨论】:

    • 谢谢你为什么写返回'test'我必须在这里写什么?
    • 我只是返回一个虚拟值。
    【解决方案3】:

    您是否尝试过运行createController();?我认为您的 NavCtrl 控制器不会被嘲笑。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2017-02-24
      • 2014-05-01
      • 2016-10-10
      • 1970-01-01
      • 2015-01-23
      • 2014-05-04
      • 1970-01-01
      相关资源
      最近更新 更多