【发布时间】:2015-12-16 00:08:19
【问题描述】:
尝试使用 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;
}
【问题讨论】:
-
您的
preprocessors密钥'*.html'可能与clientApp + '**/*.html'中的任何内容都不匹配。尝试使用相同的模式,甚至只使用'**/*.html'? -
你说得对,我实际上在你评论的同时把它改成了那个,但这似乎并没有解决它。我已经更新了我的原始帖子以显示选项对象现在的样子。
-
templateUrl属性中的路径是否包含clientApp的任何内容?例如,假设clientApp = 'myApp/src',您的templateUrl属性是否类似于'myApp/src/path/to/template.html'? -
clientApp = "./src/client/app/" templateUrl: "src/client/app/orders/' 但我也试过没有clientApp...
-
如果您使用真正的浏览器(不是 Phantom)启动 Karma,您可以查看页面源代码并查看包含的脚本文件。查看您的模板以及用于将它们添加到
$templateCache的路径/键。它们是否与templateUrl中的值匹配?
标签: javascript angularjs unit-testing karma-runner