对于控制器angualr因缘茉莉单元测试 [英] angualr karma jasmine unit test for a controller
问题描述
我增加了UserController的模块,我与茉莉花大只佬SPA项目。我得到一个错误说错误:[$喷油器:modulerr]未能实例由于模块myApp.controllers:错误:[$喷油器:NOMOD]!模块'myApp.controllers不可用。该应用程序工作正常。它只是在单元测试没有看到由于某种原因控制器模块。感谢您的帮助。
///<参考路径=〜/脚本/ _references.js />\r
///<参考路径=〜/脚本/控制器/ userController.js/>\r
\r
使用严格的;\r
\r
描述(控制器:userCtrl',函数(){\r
VAR范围,CTRL;\r
\r
beforeEach(函数(){\r
模块('myApp.controllers');\r
});\r
\r
beforeEach(注(函数($ rootScope,$控制器){\r
范围= $ rootScope $新的()。\r
CTRL = $控制器('userCtrl',{\r
$适用范围:适用范围\r
});\r
}));\r
\r
它(应该是假的,函数(){\r
期待(假).toBeFalsy();\r
});\r
}
\r
该mainController.js只包含两个用['myApp.factories']作为模块声明这样的依赖行:
使用严格的;\r
\r
angular.module('myApp.controllers',['myApp.factories']);
\r
的实际控制器是在他们自己的.js文件,和在声明具有angular.module('myApp.controllers')无[]。该userController.js是这样的:
使用严格的;\r
\r
angular.module('myApp.controllers')\r
.controller('userCtrl',['$范围,$位置,$窗口','版本',\r
功能($范围,$位置$窗口,版本)\r
{\r
。$ $范围root.title ='AngularJS SPA |用户';\r
$ scope.appVersion =版本;\r
$ scope.title ='用户';\r
$ scope.content =我的内容放在这里;\r
}]);
\r
在karma.conf.js,我在文件中的以下内容:财产
文件:\r
bower_components /角/ angular.js',\r
bower_components /角度的UI路由器/发行/角-UI-router.js',\r
bower_components /角嘲笑/角mocks.js',\r
bower_components /角资源/角resource.js',\r
bower_components / jQuery的/距离/ jquery.js和'\r
对myApp /脚本/控制器/ *。JS',\r
对myApp /脚本/测试/ *。spec.js',\r
对myApp /脚本/测试/ ** / *。spec.js\r
]
\r
这个问题来自于 myApp.factories
,我没有看到这个声明的模块中的任何地方因果报应文件守望者。控制器前行,把你的 myApp.factories
模块的路径,应该很好地工作。
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();
});
}
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']);
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";
}]);
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'
],
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.
这篇关于对于控制器angualr因缘茉莉单元测试的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!