如何在角度项目的jasmine测试中注入依赖项

Ran*_*dra 29 jasmine angularjs

这是测试规范文件:

describe('Test main controller', function(){
        it('Should initialize value to Loading', function(){
            $scope = {}
            ctrl =  new mainNavController($scope)
            expect($scope.wksp_name).toBe('Loading')
        })
    })
Run Code Online (Sandbox Code Playgroud)

这是控制器文件

function mainNavController($scope) {
    $scope.wksp_name = 'Loading...'
    $scope.$on('broadCastWkspNameEvent', function (e, args) {
        $scope.wksp_name = args
    })
}

mainNavController.$inject=['$scope']
Run Code Online (Sandbox Code Playgroud)

但我的测试失败了 Object #<Object> has no method '$on'

我正在使用茉莉花的基本设置.

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
  "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
  <title>Jasmine Spec Runner</title>

  <link rel="shortcut icon" type="image/png" href="testlib/jasmine-1.2.0/jasmine_favicon.png">
  <link rel="stylesheet" type="text/css" href="testlib/jasmine-1.2.0/jasmine.css">
  <script type="text/javascript" src="testlib/jasmine-1.2.0/jasmine.js"></script>
  <script type="text/javascript" src="testlib/jasmine-1.2.0/jasmine-html.js"></script>

  <!-- include source files here... -->
  <script type="text/javascript" src="/static_files/js/test-specs/main-nav-spec.js"></script>

  <!-- include spec files here... -->
  <script type="text/javascript" src="/static_files/js/common/jquery/latest.js"></script>
  <script type="text/javascript" src="/static_files/js/common/angular/angular-1.0.1.min.js"></script>
  <script type="text/javascript" src="/static_files/js/common/angular/angular-resource-1.0.1.min.js"></script>
  <script type="text/javascript" src="/static_files/js/section/main-nav-controller.js"></script>

  <script type="text/javascript">
    (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>

</head>

<body>
</body>
</html>
Run Code Online (Sandbox Code Playgroud)

我做错了什么?我无法理解这件事应该如何工作:)

pko*_*rce 59

测试代码的主要问题是它尝试使用new运算符"手动"创建控制器的实例.这样做时AngularJS没有机会注入依赖项.你应该做的是允许AngularJS注入依赖项:

var $scope, ctrl;

//you need to inject dependencies first
beforeEach(inject(function($rootScope) {
    $scope = $rootScope.$new();        
}));

it('Should initialize value to Loading', inject(function($controller) {
    ctrl = $controller('MainNavController', {
        $scope: $scope
    });
    expect($scope.wksp_name).toBe('Loading...');
}));
Run Code Online (Sandbox Code Playgroud)

这是一个完整的jsFiddle的链接:http://jsfiddle.net/pkozlowski_opensource/7a7KR/3/

在上面的例子中有两件值得注意的事情:

  1. 您可以使用ngMock模块中的inject()方法注入依赖项:https://docs.angularjs.org/api/ngMock/function/angular.mock.inject
  2. 要创建一个控制器实例(支持依赖注入),您将使用$ controller服务:http://docs.angularjs.org/api/ng.$controller

作为最后一句话:我建议命名控制器以大写字母开头 - 这样我们就不会将它们与变量名混淆.


zby*_*our 17

@ pkozlowski.opensource的答案很棒.详细说明......有时断言$scope.$on你的控制器确实调用了它也很方便.在这种情况下,你可以窥探$scope.$on如下:

beforeEach(inject(function($rootScope) {
    $scope = $rootScope.$new();        
    spyOn($scope, '$on').andCallThrough();
}));
Run Code Online (Sandbox Code Playgroud)

然后你可以断言$on用你的事件名称和一些函数作为参数调用:

it('Should bind to "broadCastWkspNameEvent"', inject(function($controller) {
    ctrl = $controller('MainNavController', {
        $scope: $scope
    });
    expect($scope.$on).toHaveBeenCalledWith('broadCastWkspNameEvent', jasmine.any(Function));
}));
Run Code Online (Sandbox Code Playgroud)