我不能让karma-ng-html2js-preprocessor为外部模板工作。
打包Json文件:
.....
"gulp-karma": "*",
"karma-coverage": "*",
"karma-jasmine": "*",
"karma-ng-html2js-preprocessor": "*",
"karma-phantomjs-launcher": "*",
.....Karma配置文件:
config.set({
browsers: [
....
],
frameworks: [
'jasmine'
],
plugins: [
'karma-jasmine',
'karma-phantomjs-launcher',
'karma-ng-html2js-preprocessor'
],
preprocessors: {
'app/**/*.html': 'ng-html2js'
},
ngHtml2JsPreprocessor: {
stripPrefix: 'app/'
}
});文件在构建文件中定义,并传递给gulp karma。以下是定义的文件:
config = { test: {
configFile: '.../karma.conf.js',
depends: [
.......
],
files: [
"app/**/*.js",
'app/**/*.html'
]
}
}在我的指令规范中加载模板,如下所示:
beforeEach(module('app'));
beforeEach(module('app/tem/mytemp.html'));我收到以下错误:
Error: [$injector:modulerr] Failed to instantiate module app/tem/mytemp.html due to:
Error: [$injector:nomod] Module 'app/tem/mytemp.html' is not available! You either misspelled the在karma中,debug.html html文件被加载到链接标记输出中:
<script type="text/javascript" src="/absoluteC:.../app/tem/comp/mydirective.js"></script>
<link href="/absoluteC:..../app/tem/mytemp.html" rel="import">
<script type="text/javascript">
window.__karma__.loaded();
我错过了什么吗?如何调试并解决此问题?
发布于 2014-09-09 01:01:12
下面是我是如何解决同样的问题的:
1) npm install karma-ng-html2js-preprocessor --save-dev (你已经做过了)
2)在karma.config.js中:
// ....
preprocessors: {
'**/*.html': ['ng-html2js']
},
// ....
ngHtml2JsPreprocessor: {
stripPrefix: 'app/', // <-- change as needed for the project
// include beforeEach(module('templates')) in unit tests
moduleName: 'templates'
},
plugins : [
'karma-phantomjs-launcher',
'karma-jasmine',
'karma-ng-html2js-preprocessor'
]3)由于gulp karma覆盖了karma.conf.js的files属性,因此请将测试设置的gulp任务配置(我有两个:一个test运行一次测试,另一个称为tdd以进行连续测试)更改为如下所示:
gulp.task('test', function() {
var bowerDeps = ...
var testFiles = bowerDeps.js.concat([
'app/scripts/**/*.js',
'test/unit/**/*.js',
'app/**/*.html' // <-- This is what you need to add to load the .html files
]);
return gulp.src(testFiles)
.pipe($.karma({
configFile: 'test/karma.conf.js',
action: 'run'
}))
....
});希望这能帮助到一些人。
发布于 2014-09-05 20:33:41
我也是新手,我在谷歌上搜索另一个问题,但我认为我比你走得更远(我也意识到这个探索有点古老)。
我所做的是对Karma配置文件的ngHtml2JsPreprocessor部分进行了以下更改
ngHtml2JsPreprocessor: {
stripPrefix: 'app/',
// ADDED THIS: the name of the Angular module to create
moduleName: "my.templates"
}然后,在我的测试中,我引用了该模块名称,而不是HTML名称。
beforeEach(module('my.templates'));我希望这会有帮助,即使现在已经很晚了。或者其他人在搜索时发现这些信息是有用的。
https://stackoverflow.com/questions/24300540
复制相似问题