Angularjs ngHtml2JsPreprocessor不加载包含模板的模块,使用karma进行测试
使用karma ngHtml2JsPreprocessor测试我的angular应用程序,我已经阅读了几十页关于如何配置karma来生成js模板而不是由angular指令加载的html模板的内容,我被以下消息所困扰:Angularjs ngHtml2JsPreprocessor不加载包含模板的模块,使用karma进行测试,angularjs,karma-runner,ng-html2js,Angularjs,Karma Runner,Ng Html2js,使用karma ngHtml2JsPreprocessor测试我的angular应用程序,我已经阅读了几十页关于如何配置karma来生成js模板而不是由angular指令加载的html模板的内容,我被以下消息所困扰: Module 'js_templates' is not available! You either misspelled the module name or forgot to load it. If registering a module ensure that you s
Module 'js_templates' is not available! You either misspelled the module name or forgot to load it. If registering a module ensure that you specify the dependencies as the second argument.
http://errors.angularjs.org/1.4.6/$injector/nomod?p0=js_templates
我的文件夹结构如下所示:
/System/Root/Path/
web/
js/
app/
test/
specs/
UserModule/
UserDirectives.spec.js
modules/
UserModule/
UserDirectives.js <=== Where the directive is defined
Templates
login.tpl.html
directives.directive("loginForm", [function(){
return{
restriction: "E",
templateUrl: assetsRootPath+'/Templates/login.tpl.html' //assetsRootPath is defined to '' for karma
}]);
业力配置如下:
module.exports = function(config) {
config.set({
basePath: './web/js/',
frameworks: ['jasmine', 'requirejs'],
preprocessors: {
'../Templates/**/*.html': ['ng-html2js']
},
ngHtml2JsPreprocessor: {
stripPrefix: '.*web/',
prependPrefix: '/',
moduleName: 'js_templates',
enableRequireJs: false
},
plugins : [
'karma-chrome-launcher',
'karma-jasmine',
'karma-requirejs',
'karma-ng-html2js-preprocessor'
],
// list of files / patterns to load in the browser
files: [
{ pattern: '../Templates/**/*.html', included: false },
//....Other needed files
]
测试指令的规范:
define(['angular', 'ngmocks', 'app/modules/UserModule/UserDirectives'], function() {
describe("Test User Directive > ", function () {
var $compiler, compiledElement;
beforeEach(module('User.Directives'));
beforeEach(module('js_templates'));
beforeEach(inject(function (_$rootScope_, _$compile_) {
$rootScope = _$rootScope_;
$compile = _$compile_;
scope = $rootScope.$new();
compiledElement = $compile("<login-form></login-form>")(scope);
scope.$digest();
}));
it('Tries to test', function(){
expect(1).toEqual(1);
});
});
});
我已经尝试了很多配置,但都不起作用,我还尝试通过添加一些log.debug调试ngHtml2JsPreprocessor&我看到模块“js_templates”是正确创建的,问题是为什么我不能为规范加载它。可能有用的信息(不确定它是否改变了什么):我正在使用requirejs。
感谢您的帮助好吧,由于没有任何配置工作,我选择了使用不同的方法,下面是我最终要做的工作: 1-卸下所有预处理器 2-安装简单的html2js节点插件:npm安装karma-html2js-preprocessor——保存开发 3-在我的karma.conf文件中:
pattern: { files: '../Templates/**/*.html', included : true } //Note the included property to true
preprocessors: {
'../Templates/**/*.html': ['html2js']
},
plugins : [
'karma-chrome-launcher',
'karma-jasmine',
'karma-requirejs',
'karma-html2js-preprocessor'
],
在我的test-main.js文件(require js的入口点)中,我完成了以下操作:
require([
'angular',
...
], function(angular, app) {
var $html = angular.element(document.getElementsByTagName('html')[0]);
angular.element().ready(function() {
// bootstrap the app manually
angular.bootstrap(document, ['app']);
//Making all templates to be availabe through js_template module
angular.module('js_templates', []).run(function($templateCache){
angular.forEach(window.__html__, function(content, filename){
var modifiedFilename = filename;
/*****************Modify this to fit your app**********************/
var breakPos = modifiedFilename.indexOf('/Templates');
modifiedFilename = filename.substr(breakPos, modifiedFilename.length - breakPos);
/*****************/Modify this to fit your app*********************/
$templateCache.put(modifiedFilename, content);
});
});
});
}
);
不是很锋利,但也不是很糟糕,不管怎样,它确实有效,这正是我想要的。当使用ngHtml2JsPreprocessor解决方案能够正常工作时,我将使用它
require([
'angular',
...
], function(angular, app) {
var $html = angular.element(document.getElementsByTagName('html')[0]);
angular.element().ready(function() {
// bootstrap the app manually
angular.bootstrap(document, ['app']);
//Making all templates to be availabe through js_template module
angular.module('js_templates', []).run(function($templateCache){
angular.forEach(window.__html__, function(content, filename){
var modifiedFilename = filename;
/*****************Modify this to fit your app**********************/
var breakPos = modifiedFilename.indexOf('/Templates');
modifiedFilename = filename.substr(breakPos, modifiedFilename.length - breakPos);
/*****************/Modify this to fit your app*********************/
$templateCache.put(modifiedFilename, content);
});
});
});
}
);