我在 AngularJS 中测试我的控制器时遇到了一些问题,我收到以下错误:
enter PhantomJS 1.9.7 (Linux) Controller: HeaderCtrl Should redirect on a new user form FAILED
Error: Injector already created, can not register a module!
at workFn (/path/ui/bower_components/angular-mocks/angular-mocks.js:2038)
Error: [ng:areq] Argument 'HeaderCtrl' is not a function, got undefined
http://errors.angularjs.org/1.3.0-beta.5/ng/areq?p0=HeaderCtrl&p1=not%20a%20function%2C%20got%20undefined
at assertArg (/path/ui/bower_components/angular/angular.js:1443)
at assertArgFn (/path/ui/bower_components/angular/angular.js:1454)
at /path/ui/bower_components/angular/angular.js:7134
at /path/test/ui/unit/core/controllers/header.js:37
at invoke (/path/ui/bower_components/angular/angular.js:3873)
at workFn (/path/ui/bower_components/angular-mocks/angular-mocks.js:2171)
undefined
这是我的测试:
describe('Controller: HeaderCtrl', function () {
//<--------------------------------------------------------------------------->
//- Define the before/after class/test behaviour
//<--------------------------------------------------------------------------->
// load the controller's module
beforeEach( module('MyApp') );
var HeaderCtrl,
authService,
scope;
// define the mock Auth service
beforeEach( function() {
authService = {
auth: function() {}
};
});
// Initialize the controller and a mock scope
beforeEach( inject(function ($controller, $rootScope) {
scope = $rootScope.$new();
HeaderCtrl = $controller('HeaderCtrl', {
$scope: scope,
auth: authService
});
}));
//<--------------------------------------------------------------------------->
//- Tests
//<--------------------------------------------------------------------------->
describe('On a correct login for a new user', function() {
it('Should redirect on a new user form', function () {
});
});
});
我的 HeaderCtrl 看起来像这样:
angular.module('MyApp')
.controller('HeaderCtrl', function ($scope, $http, $window, $interval, $location, error, core, auth) {
$scope.user = auth.user;
$scope.login = function () {
auth.auth.query(function (data) {
// doing some stuff
});
};
$scope.logged = false;
});
该控制器正在使用一些服务,这些服务在其他模块中定义,如下所示:
var services = angular.module('MyApp.services', ['ngResource']);
services.factory('auth', function ($resource, core) {
var factory = {};
factory.auth = $resource('/an/api/rest/service', {}, {
query: {method: 'GET', params: {}, isArray: false}
});
factory.user = {};
return factory;
});
并根据要求应用程序定义:
angular
.module('MyApp', [
'ngCookies',
'ngResource',
'ngSanitize',
'ngRoute',
'MyApp.services'
])
.config(function ($locationProvider) {
$locationProvider.html5Mode(false);
})
.config(function ($routeProvider) {
$routeProvider
.when('/', {
templateUrl: 'views/core/main.html',
controller: 'MainCtrl'
})
.otherwise({
redirectTo: '/'
});
});
我在我的视图中使用我的 HeaderCtrl 直接:
<div ng-controller="HeaderCtrl" ng-include="'views/core/fragments/header.html'"></div>
似乎我的控制器没有被 karma 加载,但是我检查了我的 karma conf 文件并加载了所有 .js 文件......我什至首先加载了 app.js 以确保创建了模块:
files: [
'../ui/bower_components/angular/angular.js',
'../ui/bower_components/angular-mocks/angular-mocks.js',
'../ui/bower_components/angular-resource/angular-resource.js',
'../ui/bower_components/angular-cookies/angular-cookies.js',
'../ui/bower_components/angular-sanitize/angular-sanitize.js',
'../ui/bower_components/angular-route/angular-route.js',
'../ui/scripts/app.js',
'../ui/scripts/modules.js',
'../ui/scripts/**/*.js',
'../test/ui/**/*.js'
],
我错过了什么?我被困住了:'(
提前谢谢你的帮助:)
扬尼