控制器的角业茉莉花单元测试 [英] angular karma jasmine unit test for a controller
问题描述
我正在使用Karma上的Jasmine将userController模块添加到我的SPA项目中.我收到一条错误消息,指出错误:[$ injector:modulerr]由于以下原因而无法实例化模块myApp.controllers:错误:[$ injector:nomod]模块'myApp.controllers'不可用!".该应用程序运行正常.只是由于某种原因,单元测试看不到控制器模块.感谢您的帮助.
I am adding a userController module to my SPA project with Jasmine on Karma. I got an error saying "Error: [$injector:modulerr] Failed to instantiate module myApp.controllers due to: Error: [$injector:nomod] Module 'myApp.controllers' is not available!". The app works fine. It just the unit test doesn't see the controller module for some reason. Thanks for your help.
/// <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']作为依存关系,如下所示:
The mainController.js contains only two lines with ['myApp.factories'] as dependency in the module declaration like this:
'use strict';
angular.module('myApp.controllers', ['myApp.factories']);
实际的控制器位于它们自己的.js文件中,并且具有在声明中不带[]的angular.module('myApp.controllers').userController.js就像这样:
The actual controllers are in their own .js files, and have angular.module('myApp.controllers') without [ ] in the declaration. The userController.js is like this:
'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中,文件中包含以下内容:
In karma.conf.js, I have the following in the files: property.
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'
],
推荐答案
问题来自 myApp.factories
,我在业力文件观察器的任何地方都看不到此声明的模块.在控制器行之前,放置您的 myApp.factories
模块的路径,并且应该可以正常工作.
The issue comes from myApp.factories
, I don't see this declared module anywhere in the karma files watcher. Before the controllers line, put the path of your myApp.factories
module and should work fine.
这篇关于控制器的角业茉莉花单元测试的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!