我使用内联模板创建了一个指令,对其进行了测试,一切正常。现在我将模板放入单独的.html文件中,并通过templateUrl
从指令引用它。指令适用于我的页面,但是测试被打破了:
Error: [$injector:modulerr] Failed to instantiate module source/html/par
tials/template-directive-my-directive.html due to:
Error: [$injector:nomod] Module 'source/html/partials/template-directive
-my-directive.html' is not available! You either misspelled the module name
or forgot to load it. If registering a module ensure that you specify the depend
encies as the second argument.
我使用ng-html2js preprocessor没有运气(上面的错误),在我的karma配置和单元测试模块加载中尝试不同的路径和前缀。任何人都可以看到什么是错的(哪条路径,或者可能是不同的东西?)?
我的结构是:
ui/Gruntfile.js // contains the karma config
ui/source/html/index.html
ui/source/html/partials/template-directive-my-directive.html
ui/source/js/my-directive.js
ui/source/tests/unit/unit-my-directive.js
Gruntfile.js内部:
karma : {
unit : {
options : {
files : [
'source/lib/angular/angular.js', // angular first
'source/lib/angular/*.js', // then any other angular files...
'source/lib/x2js/xml2json.min.js',
'source/lib/jquery/jquery.js',
'source/lib/ui-bootstrap/ui-bootstrap.js',
'source/js/*.js',
'source/tests/unit/*.js',
'source/html/partials/*.html', // because of directive templates
],
autoWatch : true,
frameworks : ['jasmine', 'detectBrowsers'],
plugins : [
'karma-junit-reporter',
'karma-jasmine',
'karma-chrome-launcher',
'karma-firefox-launcher',
'karma-ie-launcher',
'karma-safari-launcher',
'karma-opera-launcher',
'karma-phantomjs-launcher',
'karma-detect-browsers'
],
// generate js files from html templates to expose them during testing
preprocessors : {
'source/html/partials/*.html' : 'ng-html2js'
}
// ,
// ngHtml2JsPreprocessor : {
// stripPrefix : 'source/html/', // tried this, no luck
// moduleName: 'foo' // tried this, no luck
// }
}
}
}
我的测试内部(unit-my-directive.js):
// load the template
beforeEach(module('source/html/partials/template-directive-my-directive.html'));
在我的指令(my-directive.js)中:
templateUrl : 'partials/template-directive-my-directive.html', // this is ok, because actual app works
从ui
文件夹中运行测试,Gruntfile.js所在的文件夹
答案 0 :(得分:17)
<强>解决方案:强> 在我花了几个小时之后,ST answer帮助了我。我只需要在karma插件中注册ng-html2js
plugins : [
'karma-junit-reporter',
'karma-jasmine',
'karma-chrome-launcher',
'karma-firefox-launcher',
'karma-ie-launcher',
'karma-safari-launcher',
'karma-opera-launcher',
'karma-phantomjs-launcher',
'karma-detect-browsers',
'karma-ng-html2js-preprocessor' //added this
],