Javascript 单元测试一个指令,其模板都是带有脚本标记的文件
我很难弄清楚如何在我的Karma单元测试中包含指令的模板(它们都在一个文件中,位于不同的脚本标记中) 我得到的错误是:Javascript 单元测试一个指令,其模板都是带有脚本标记的文件,javascript,unit-testing,angularjs,karma-runner,Javascript,Unit Testing,Angularjs,Karma Runner,我很难弄清楚如何在我的Karma单元测试中包含指令的模板(它们都在一个文件中,位于不同的脚本标记中) 我得到的错误是: phantomjs1.9(Linux)错误 语法错误:分析错误 在/var/www/html/tweak/core/global/views/js/modules/datable/templates.html:1 PhantomJS 1.9(Linux):执行0/0错误(0.313秒/0秒) 以下是守则的相关部分: 我的指令是: return { scope
phantomjs1.9(Linux)错误
语法错误:分析错误
在/var/www/html/tweak/core/global/views/js/modules/datable/templates.html:1
PhantomJS 1.9(Linux):执行0/0错误(0.313秒/0秒)
以下是守则的相关部分:
我的指令是:
return {
scope : {
columns : '=',
config : '='
},
templateUrl : 'datable/table.html',
restrict : 'E',
controller : 'datableCtrl',
link : linkingFunction
};
我的模板文件:
<script type="text/ng-template" id="datable/table.html">
<!-- data rows -->
<tr
ng-repeat="row in rows track by $id($index)"
class="datable-row"
ng-hide="loading">
<td
ng-repeat="column in columns track by $id($index)"
ng-class="{'edit-on': editMode == 'on'}"
class="{{column.classes.join(' ') + ' column' + $index}}"
ng-style="column.style">
<div ng-include="editMode == 'on' && column.editable
? 'datable/editCell.html'
: 'datable/normalCell.html'">
</div>
</td>
<!-- save button -->
<td ng-show="editMode == 'on'" style="width:1px;">
<button class="btn"> Save </button>
</td>
<!-- / save button -->
</tr>
<!-- / data rows -->
</script>
<script type="text/ng-template" id="datable/editCell.html">
<div ng-switch="column.inputType">
<!-- text input -->
<div ng-switch-when="text">
<div ng-class="{
'input-append' : column.append != '',
'input-prepend' : column.prepend != ''
}">
<span
class="add-on"
ng-show="column.prepend"> {{column.prepend}} </span>
<input
type="text"
ng-model="row[column.model]"
ng-keydown="query()"
ng-class="inputClass.join(' ')"
ng-attrs="column.inputAttrs">
<span
class="add-on"
ng-show="column.append"> {{column.append}} </span>
</div>
</div>
<!-- end text input -->
<!-- select input -->
<div ng-switch-when="select">
<select
ng-model="row[column.model]"
ng-change="query()"
ng-options="item.value as item.name for item in column.options"
ng-class="inputClass.join(' ')"
ng-attrs="column.inputAttrs">
<option value=""> -- </option>
</select>
</div>
<!-- end select -->
<!-- radio / checkbox -->
<div ng-switch-default>
<label ng-repeat="(key, value) in column.options track by $id($index)">
<input
type="{{column.inputType}}"
ng-class="inputClass.join(' ')"
ng-change="query()"
value="{{key}}"
ng-checked="row[column.model].indexOf('key') > -1"
ng-attrs="column.inputAttrs">
<span> {{value}} </span>
</label>
</div>
<!-- end radio / checkbox -->
</div>
</script>
<script type="text/ng-template" id="datable/normalCell.html">
<div class="read-only">
<span> {{column.prepend}} </span>
<!-- <span> {{row[column.model] | datableFilter : column.filter}} </span> -->
<span ng-bind-html-unsafe="(row[column.model] + '') | datableFilter : column.filter"></span>
<span> {{column.append}} </span>
</div>
</script>
我认为您的错误是因为您试图将HTML文件加载到通常接受javascript的文件列表中。不过,我确实有一个解决办法 在我开始之前,我有业力0.10.2,看起来你在0.8.x或以下?我在0.10.2中有此功能,但无法安装0.8.x。我将尝试为0.8.x进行翻译,但无法测试我正在做什么,因此我将主要用0.10.x进行描述。不管怎样,如果你有能力的话,转移到最近的业力可能会更容易 配置 0.10.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);
}));
可以通过加载外部HTML部分。这通常用于通过templateUrl
和类似方法直接在指令中加载。在0.10.2中,您需要确保已安装此软件包(使用npm),然后在karma配置中包括以下内容:
var branch = 'tweak';
basePath = '/var/www/html/' + branch + '/';
files = [
// Dependencies
JASMINE,
JASMINE_ADAPTER,
'https://ajax.googleapis.com/ajax/libs/jquery/2.0.0/jquery.min.js',
'https://ajax.googleapis.com/ajax/libs/angularjs/1.1.5/angular.min.js',
'http://code.angularjs.org/1.1.5/angular-mocks.js',
// other requirements
'core/global/views/js/modules/rest/module.js',
// the project source
'core/global/views/js/modules/datable/module.js',
'core/global/views/js/modules/datable/values.js',
'core/global/views/js/modules/datable/services.js',
'core/global/views/js/modules/datable/filters.js',
'core/global/views/js/modules/datable/directives.js',
'core/global/views/js/modules/datable/controllers.js',
'core/global/views/js/modules/datable/*.html',
// my spec suite
'core/global/views/js/modules/datable/tests.js'
];
exclude = [
];
reporters = ['progress'];
port = 9876;
runnerPort = 9100;
colors = true;
logLevel = LOG_INFO;
autoWatch = true;
browsers = ['PhantomJS'];
captureTimeout = 60000;
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
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。。。我认为您需要使用html2js
,它不是很强大,但在这个版本中包含在karma中。您不需要安装或将其包含在插件中,也无法配置如何使用它,所以您只需要
preprocessors = { '**/*.html': ['html2js'] }
创建的模块及其插入到$templateCache
中的项将使用用于引用主模板html的路径命名
Javascript
0.10.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);
}));
现在,您应该能够加载相关模块,并使用
var templates = $templateCache.get('inlinetemplates')
剩下要做的就是将内联模板从主模板文件内容推送到$templateCache
。这是使用angularscript
指令完成的,因此我们只需要编译/链接使用angular加载的文件。您只需使用
$compile(templates)(scope);
因此,将这些放在一起,您可以在需要加载模板的任何description
块中包含以下内容
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片段推送到测试中的功能,所缺少的只是能够正确解释主模板。我也以这种方式制作模板,还没有找到测试解决方案。因为这个问题,我只测试服务,但想知道更多。有人试过吗?我想这是一个会被支持的东西?如此详细的描述,做得好,谢谢