我用内联模板创建了一个指令,并对其进行了测试,一切都很好。现在,我已经将模板放入分离的.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预处理器 (上面的错误),在业力配置和单元测试模块加载中尝试不同的路径和前缀。有人能看到什么是错的吗(哪条路,或者什么不同的东西?)
我的结构是:
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驻留在该文件夹中
发布于 2014-04-11 09:09:36
解决方案:在我花了几个小时之后,这个ST回答帮助了我。我只需要在业力插件中注册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
],https://stackoverflow.com/questions/22966697
复制相似问题