Angularjs + karma模块MainCtrl中的许多控制器

时间:2014-04-15 09:37:47

标签: angularjs unit-testing jasmine karma-runner karma-jasmine

我在MainCtrl.js文件中有一个名为MainCtrl的模块

angular.module("MainCtrl",['ui.bootstrap']);

我正在尝试完成Jasmine测试(使用Karma)来验证控制器。理想情况下,我的应用程序只需要一个模块MainCtrl和控制器,然后让我将所有控制器分成不同的文件。

我在ht文件loginCtrl.js

中有其他文件中的控制器,如loginCtrl
angular.module('MainCtrl').controller("LoginCtrl", ['$scope','Factory',
    function ($scope, Factory) {

            //code
        }
]);

在我的单元测试中,我试试这个:

'use strict';

describe("Controllers", function() {

  beforeEach(module('App'));
  beforeEach(module('MainCtrl'));
  beforeEach(module('services'));

  describe("Unit Test LoginCtrl", function() {

    var ctrl, Factory, scope, $compile , $httpBackend;

    beforeEach(inject(function(_$compile_, _$httpBackend_, $rootScope, $controller,$injector) {
      $httpBackend = _$httpBackend_;
      $compile = _$compile_;

      Factory = $injector.get('Factory');

      scope = $rootScope.$new();
      ctrl = $controller("LoginCtrl", {$scope: scope});

    }));

    it('should have a ReportIndividual controller', function() {
      expect(ctrl).toBeDefined();
    });
  });
});

app.js

var app = angular.module('App', [
    'MainCtrl',
    'services',
]);

我的karma.conf.js

files: [
    'jasmine.js',
    'adapter.js',
    'angular.js',
    'angular-route.js',
    'angular-mocks.js',
    'jquery.min.js',
    'app.js',
    {pattern: 'scripts/*.js', watched: true, included: true, served: true},
    {pattern: 'tests/unit/*Spec.js', watched: true, included: true, served: true}
],

当我开始测试时,我在浏览器控制台中收到此消息:

"minErr/<@http://localhost:9876/base/angular.js:78
loadModules/<@http://localhost:9876/base/angular.js:3703
forEach@http://localhost:9876/base/angular.js:322
loadModules@http://localhost:9876/base/angular.js:3668
createInjector@http://localhost:9876/base/angular.js:3608
workFn@http://localhost:9876/base//angular-mocks.js:2139

当我将[]添加到声明模块时,测试工作但不是app :(:

 angular.module('MainCtrl',[]).controller("LoginCtrl", ['$scope','Factory',
    function ($scope, Factory) {

            //code
        }
]);

寻求帮助:)

1 个答案:

答案 0 :(得分:0)

angular.module('MainCtrl',[]).controller("LoginCtrl", ['$scope','Factory',
    function ($scope, Factory) {

        //code
    }
]);

以上定义是正确的。添加&#39; []&#39;?

后会出现什么错误