正在尝试使用karma-ng-html2js-preprocessor。Karma发现我所有的其他东西(javascript)都很好,但当涉及到这个html预处理器时,它似乎无法找到并从中生成模块
查看下面的选项对象。正如您所看到的,我已经向files属性添加了一个模式,并且我正在为模块使用通用名称"foo"。在测试中,每当我尝试调用module('foo');
时,因果报应都会抛出一个错误我真的希望能够使用它,这样我就不必将模板硬编码到我的单元测试或其他一些古怪的解决方案中。
var options = {
files: [].concat(
bowerFiles,
config.specHelpers,
clientApp + '**/*.module.js',
clientApp + '**/*.js',
clientApp + '**/*.html',
'*.html',
temp + config.templateCache.file,
config.serverIntegrationSpecs
),
exclude: [],
coverage: {
dir: report + 'coverage',
reporters: [
// reporters not supporting the `file` property
{ type: 'html', subdir: 'report-html' },
{ type: 'lcov', subdir: 'report-lcov' },
{ type: 'text-summary' } //, subdir: '.', file: 'text-summary.txt'}
]
},
ngHtml2JsPreprocessor: {
// strip this from the file path
// stripPrefix: clientApp,
moduleName: 'foo'
},
preprocessors: {}
};
options.preprocessors[clientApp + '**/*.html'] = ['ng-html2js'];
options.preprocessors[clientApp + '**/!(*.spec)+(.js)'] = ['coverage'];
return options;
}
我知道这不一定能回答你的问题,如果有更多信息曝光,我会添加到这个答案中,但我发现这个策略效果很好。。。
我发现指令、路由或状态的HTML模板(如果使用ui-router)在单元测试时不应该发挥作用。单元测试就是测试组件的接口;其公开可用的方法和属性。测试与HTML文档的交互实际上是使用Protractor之类的东西进行端到端测试的领域。
考虑到这一点,以下是我如何防止Karma在测试期间尝试加载模板文件。
beforeEach(function() {
module('module.name.of.the.testable.component');
inject(function($templateCache) {
$templateCache.put('path/to/template/as/it/appears/in/your/component.html',
'<div></div>');
});
});