Директива с templateUrl - тестирование с помощью ng-html2js
Я создал директиву со встроенным шаблоном, проверил его, все работало нормально. Теперь я поместил шаблон в отдельный файл.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 (ошибка выше), пробуя разные пути и префиксы в моей конфигурации 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
1 ответ
Решение: после того, как я провел часы, этот ответ 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
],