【发布时间】:2015-07-02 20:59:59
【问题描述】:
我正在使用 Jasmine on Karma 将 userController 模块添加到我的 SPA 项目中。我收到一条错误消息,提示“错误:[$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();
});
}
mainController.js 在模块声明中仅包含两行 ['myApp.factories'] 作为依赖项,如下所示:
'use strict';
angular.module('myApp.controllers', ['myApp.factories']);
实际的控制器在他们自己的 .js 文件中,并且在声明中没有 [ ] 的 angular.module('myApp.controllers') 。 userController.js 是这样的:
'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";
}]);
在 karma.conf.js 中,我在文件中有以下内容:属性。
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'
],
【问题讨论】:
-
你在使用 require / browserify / webpack 吗?