[英]Karma Jasmine Angular Error: [$injector:nomod]
Short version: When I run karma, angular is telling me it cannot inject my 'common' module. 简短版本:运行业力时,angular告诉我它无法注入“通用”模块。 The actual error is: 实际错误是:
Error: [$injector:nomod] http://errors.angularjs.org/1.5.5/$injector/nomod?p0=common
Here's the details: 详细信息如下:
My Karma test.js 我的业力test.js
"use strict";
describe('Testing ModuleToTest',function(){
beforeEach(module('ModuleToTest'));
var $scope = {};
var factory = {...make up stuff...}
// inject - access angular controller
beforeEach(inject([
'$controller','$rootScope',
function($ctrl,$rs){
$scope = $rs.$new();
$ctrl('ModuleToTestCtrl',{
$scope:$scope,
CommonFactory:factory
});
}]));
it('should define a someStuff object',function(){
expect(testScope.someStuff).toBeDefined();
});
});
My ModuleToTest definition: 我的ModuleToTest定义:
module.js file: module.js文件:
angular.module('ModuleToTest',[]);
controller.js file: controller.js文件:
angular.module('ModuleToTest')
.controller('ModuleToTestCtrl',
['$scope','$http','CommonFactory',
function($scope,factory){
$scope.someStuff = ['foo','bar'];
}]);
The module that defines the CommonFactory : 定义CommonFactory的模块:
module.js file: module.js文件:
angular.module('common',[]);
CommonFactory.js CommonFactory.js
angular.module('common')
.factory('CommonFactory',['$http',function($http){
// do factory stuff here
}]);
Application module defintion, app.js: 应用程序模块定义,app.js:
angular.module('MyMainApp',['ModuleToTest', ... , 'common']);
karma.config.js: karma.config.js:
// frameworks to use
// available frameworks: https://npmjs.org/browse/keyword/karma-adapter
frameworks: ['jasmine'],//'requirejs'],
// list of files / patterns to load in the browser
files: [
{pattern: 'js/vendor/angular/*.js'},
{pattern: 'js/common/*.js'},
{pattern: 'moduleToTest/js/*.js'},
{pattern: 'moduleToTest/*.html'},
{pattern: 'js/*.js'},
{pattern: 'index.html'},
{pattern: 'test/test.js'}
],
// list of files to exclude
exclude: [
'js/vendor/bootstrap*.js',
'js/vendor/jquery*.js'
],
Quick caveate: MyMainApp works as I expect it to. 快速警告:MyMainApp可以正常运行。 I am just beginning to use Karma to build unit tests for my applications modules. 我刚刚开始使用Karma为我的应用程序模块构建单元测试。 Eventually I will build the unit tests for MyMainApp, but I'm trying to start small. 最终,我将为MyMainApp构建单元测试,但我尝试从小做起。
Any suggestions or clarifications are welcome, in addition to answers! 除了答案,任何建议或澄清都欢迎! Thank you! 谢谢!
UPDATE : After helpful responses (thank you by the way!) In short, I'm getting the same error, but I've tried to simplify the problem. 更新 :经过有用的答复(谢谢您!)总之,我遇到了相同的错误,但是我试图简化问题。
I removed any mention of 'common' from my ModuleToTest. 我从ModuleToTest中删除了对“ common”的任何提及。 And Karma is still complaining about 'common'. Karma仍在抱怨“常见”。 Here's the details: 详细信息如下:
My Karma test.js 我的业力test.js
"use strict";
describe('Testing ModuleToTest',function(){
var testScope = {};
beforeEach(function(){
module('ModuleToTest');
inject(function($rootScope,$controller){
testScope = $rootScope.$new();
$controller('ModuleToTestCtrl',{
$scope: testScope
});
});
});
// No it statement for now because the module won't even load
});
My ModuleToTest definition: 我的ModuleToTest定义:
module.js file ( notice no reference to CommonFactory ): module.js文件( 注意未引用CommonFactory ):
angular.module('ModuleToTest',[]);
controller.js file ( notice the removal of CommonFactory ): controller.js文件( 注意CommonFactory的删除 ):
angular.module('ModuleToTest')
.controller('ModuleToTestCtrl',
['$scope',function($scope){
$scope.someStuff = ['foo','bar'];
}]);
ANSWER : Given the simplified version, the solution was to put angular-mocks.js after angular.min.js in the karma.conf.js file. 答 :考虑到简化版本,该解决方案是把角mocks.js在karma.conf.js文件angular.min.js后。
I hope this saves somebody else the trouble. 我希望这可以省去别人的麻烦。
You should have "common" as a dependency to "ModuleToTest" as that is where its components are used, ie 您应该将“ common”作为对“ ModuleToTest”的依赖项,因为这是使用其组件的位置,即
angular.module('ModuleToTest', ['common']);
For your test, you should really be creating a mock CommonFactory
for complete collaborator isolation. 为了进行测试,您实际上应该创建一个模拟CommonFactory
来完全隔离协作者。
describe('Testing ModuleToTest', function() {
var testScope, CommonFactory;
beforeEach(function() {
CommonFactory = {}; // or whatever mock implementation you need
module('ModuleToTest');
inject(function($rootScope, $controller) {
testScope = $rootScope.$new();
$controller('ModuleToTestCtrl', {
$scope: testScope,
CommonFactory: CommonFactory
});
});
})
// specs go here
});
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.