我认为你的错误是因为你试图将 HTML 文件加载到通常接受 javascript 的文件列表中。不过我确实有一个解决方案给你。
在开始之前,我有 karma 0.10.2,看起来您使用的是 0.8.x 或更低版本?我可以在 0.10.2 中使用此功能,但无法安装 0.8.x。我将尝试翻译 0.8.x,但无法测试我正在做的事情,因此我将主要用 0.10.x 进行描述。如果可以的话,无论如何,转移到最新的业力可能会更容易。
Config
0.10.x
外部 HTML 部分可以通过以下方式加载karma-ng-html2js-预处理器 https://github.com/karma-runner/karma-ng-html2js-preprocessor。这通常用于通过直接加载指令templateUrl
和类似的方法。在 0.10.2 中,您需要确保安装此软件包(使用 npm),然后在 karma 配置中包含以下内容:
preprocessors: {
'**/*.html' : ['ng-html2js']
},
ngHtml2JsPreprocessor: {
cacheIdFromPath: function(filepath) {
// If you had more than one html file you would want to do something more clever here.
return 'inlinetemplates';
},
moduleName: 'inlinetemplates'
},
plugins: [
...,
'karma-ng-html2js-preprocessor'
],
files: [
...,
'app/alltemplates.html', // your main template html
// Don't include paths for individual files that are inlined in the file above
]
这将允许您加载模块module('inlinetemplates')
这会将主模板文件(而不是单个模板)的内容插入到$templateCache
.
0.8.x
所以,翻译为 0.8.x...我认为你需要使用html2js
虽然不是那么强大,但在此版本中包含在业力中。您不需要安装它或将其包含在插件中,并且无法配置它的使用方式,因此您只需要
preprocessors = { '**/*.html': ['html2js'] }
创建的模块及其插入的项目$templateCache
将使用您用来引用主模板 html 的路径来命名。
JavaScript
0.10.x
现在您应该能够加载相关模块并使用以下命令访问主模板文件的内容
var templates = $templateCache.get('inlinetemplates')
剩下要做的就是将内联模板从主模板文件内容推送到$templateCache
。这是使用角度完成的script
指令,所以我们只需要编译/链接我们用 Angular 加载的文件。你可以非常简单地做到这一点
$compile(templates)(scope);
因此,将这些放在一起,您可以在任何内容中包含以下内容describe
需要加载模板的块。
beforeEach(module('inlinetemplates'));
beforeEach(inject(function($compile, $templateCache, $rootScope) {
var templatesHTML = $templateCache.get('inlinetemplates');
$compile(templatesHTML)($rootScope);
}));
0.8.x
var mainTemplateLocation = 'path/used/to/refer/to/main/templates/in/karma/conf.html';
beforeEach(module(mainTemplateLocation));
beforeEach(inject(function($compile, $templateCache, $rootScope) {
var templatesHTML = $templateCache.get(mainTemplateLocation);
$compile(templatesHTML)($rootScope);
}));
加起来
再次强调,我不能保证 0.8.x 指令能够工作,尤其是在不进行调整的情况下,但这在 0.10.x 中肯定有效。
Karma 已经具备将外部 HTML 部分推送到测试中的功能,所缺少的只是能够正确解释您的主模板。