控制器的角度业力茉莉花单元测试

时间:2015-07-02 21:00:00

标签: angularjs unit-testing

我正在将一个userController模块添加到我的SPA项目中使用Jasma on Karma。我得到一个错误说"错误:[$ injector:modulerr]由于以下原因无法实例化模块myApp.controllers:错误:[$ injector:nomod]模块' myApp.controllers'不可用!"。该应用程序工作正常。只是单元测试由于某种原因没有看到控制器模块。谢谢你的帮助。



/// <reference path="~/scripts/_references.js" />
/// <reference path="~/scripts/controllers/userController.js" />

'use strict';

describe('Controllers: userCtrl', function() {
      var scope, ctrl;

      beforeEach(function() {
        module('myApp.controllers');
      });

      beforeEach(inject(function($rootScope, $controller) {
        scope = $rootScope.$new();
        ctrl = $controller('userCtrl', {
          $scope: scope
        });
      }));

      it("Should be false", function() {
        expect(false).toBeFalsy();
      });
    }
&#13;
&#13;
&#13;

mainController.js只包含两行[&#39; myApp.factories&#39;]作为模块声明中的依赖项,如下所示:

&#13;
&#13;
'use strict';

angular.module('myApp.controllers', ['myApp.factories']);
&#13;
&#13;
&#13;

实际的控制器在他们自己的.js文件中,并且声明中没有[]的angular.module(&#39; myApp.controllers&#39;)。 userController.js是这样的:

&#13;
&#13;
'use strict';

angular.module('myApp.controllers')
  .controller('userCtrl', ['$scope', '$location', '$window', 'version',
    function ($scope, $location, $window, version)
    {
      $scope.$root.title = 'AngularJS SPA | User';
      $scope.appVersion = version;
      $scope.title = 'User';
      $scope.content = "my contents goes here";
    }]);
&#13;
&#13;
&#13;

在karma.conf.js中,我在文件中有以下内容:property。

&#13;
&#13;
files: [
          'bower_components/angular/angular.js',
          'bower_components/angular-ui-router/release/angular-ui-router.js',
          'bower_components/angular-mocks/angular-mocks.js',
          'bower_components/angular-resource/angular-resource.js',
          'bower_components/jquery/dist/jquery.js',
          'myApp/scripts/controllers/*.js',
          'myApp/scripts/tests/*.spec.js',
          'myApp/scripts/tests/**/*.spec.js'
        ],
&#13;
&#13;
&#13;

2 个答案:

答案 0 :(得分:0)

问题来自__init__,我没有在karma文件观察器的任何地方看到这个声明的模块。在控制器行之前,放置myApp.factories模块的路径,应该可以正常工作。

答案 1 :(得分:0)

我的Karma.conf.js现在有以下但现在Karma抱怨错误:[$ injector:itkn]错误的注入令牌!预期的服务名称为字符串,未定义。

files: [
  'bower_components/angular/angular.js',
  'bower_components/angular-mocks/angular-mocks.js',
  'bower_components/angular-resource/angular-resource.js',
  'bower_components/angular-ui-router/release/angular-ui-router.js',
  'bower_components/jquery/dist/jquery.js',
  'AngularSPA/Scripts/factories/services.js',
  'AngularSPA/Scripts/factories/factories.js',
  'AngularSPA/scripts/controllers/mainController.js',
  'AngularSPA/scripts/controllers/*.js',
  'AngularSPA/scripts/tests/*.spec.js',
  'AngularSPA/scripts/tests/**/*.js'
],