尝试使用善缘-NG-html2js- preprocessor。噶已经找到了我所有的其他的东西(JavaScript)的罚款,但是当它涉及到这个网站preprocessor它似乎无法找到,并从它生成一个模块。
看看下面我的选择对象。正如你所看到的,我添加了一个模式,将文件属性,我使用该模块的通用名称'富'。在测试中,业力抛出每当我尝试打电话给模块('富')的错误;
我真的希望能够利用这个,所以我不必硬code模板到我的单元测试或其他一些古怪的解决方案。
VAR选项= { 文件:[] .concat( bowerFiles, config.specHelpers, clientApp +** / *。module.js', clientApp +'** / *。JS', clientApp +'** / *。HTML, *。html的', 温度+ config.templateCache.file, config.serverIntegrationSpecs ) 排除:[], 覆盖范围:{ 导演:报告+'覆盖', 记者: //记者不支持`file`财产 {类型:HTML,子目录:报告-HTML}, {类型:'LCOV',子目录:报告-LCOV}, {类型:文本摘要} //,子目录:,文件'。':'文本的Summary.txt'} ] }, ngHtml2Js preprocessor:{ //从文件路径剥离这种 //带preFIX:clientApp, MODULENAME:'富' }, preprocessors:{} }; 。选择preprocessors [clientApp +'。** / * HTML'] = ['NG-html2js']; 选项preprocessors [clientApp +'** /(*规格)+!(JS)'] = ['覆盖']。 返回选项;}
解决方案
我知道这并不回答你的问题,如果更多信息被曝光,我会添加到这个答案,但我发现这个战略工程嗯......
我发现一个指令,路由或状态的HTML模板(如果使用的 UI路由器的)不应该开始发挥作用时的单元测试。单元测试是关于测试接口组件;其公开提供的方法和属性。与HTML文档检测的相互作用是真的端至端的测试用类似量角器领域
考虑到这一点,这里就是我的prevent噶从试图测试过程中加载模板文件。
beforeEach(函数(){ 模块('module.name.of.the.testable.component'); 注(函数($ templateCache){ $ templateCache.put('路径/要/模板/ AS /它/显示/中/你/ component.html', '< DIV>< / DIV>'); });});
Attempting to use the karma-ng-html2js-preprocessor. Karma has been finding all of my other stuff (javascript) fine, but when it comes to this html preprocessor it can't seem to find and generate a module from it.
Check out my options object below. As you can see, I've added a pattern to the files attribute and I'm using the generic name 'foo' for the module. In the test, karma throws an error whenever I try to call module('foo');
I really want to be able to use this so I don't have to hardcode templates into my unit tests or some other wacky solution.
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;
}
解决方案
I know this doesn't necessarily answer your question and if more information comes to light, I'll add to this answer but I've found this strategy works well...
I find that the HTML template of a directive, route or state (if using ui-router) shouldn't come into play when unit testing. Unit testing is about testing the interface to your component; its publicly available methods and properties. Testing the interactions with the HTML document is really in the realm of end-to-end testing with something like Protractor.
With that in mind, here's how I prevent Karma from attempting to load template files during testing.
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>');
});
});