Ron*_*per 6 javascript unit-testing angularjs karma-runner
我很难搞清楚如何在我的Karma单元测试中包含我的指令模板(这些模板都在不同脚本标签中的一个文件中).
我得到的错误:
PhantomJS 1.9 (Linux) ERROR
SyntaxError: Parse error
at /var/www/html/tweak/core/global/views/js/modules/datable/templates.html:1
PhantomJS 1.9 (Linux): Executed 0 of 0 ERROR (0.313 secs / 0 secs)
Run Code Online (Sandbox Code Playgroud)
以下是代码的相关部分:
我的指示肉:
return {
scope : {
columns : '=',
config : '='
},
templateUrl : 'datable/table.html',
restrict : 'E',
controller : 'datableCtrl',
link : linkingFunction
};
Run Code Online (Sandbox Code Playgroud)
我的模板文件:
<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>
Run Code Online (Sandbox Code Playgroud)
我的单元测试:
'use strict'
describe("datable", function() {
describe('directive', function () {
var $rootScope, $compile, element;
beforeEach(module('datable'));
beforeEach(module('/var/www/html/tweak/core/global/views/js/modules/datable/templates.html'));
beforeEach(inject(function (_$rootScope_, _$compile_) {
$rootScope = _$rootScope_;
$compile = _$compile_;
$rootScope.tableConfig = {
editable : true
};
$rootScope.columns = [];
element = angular.element('<datable config="tableConfig" columns="columns"></datable>');
$compile(element)($rootScope);
$rootScope.$digest();
}));
it('should have ng-scope class', function() {
expect(element.hasClass('ng-scope')).toBe(true);
});
});
});
Run Code Online (Sandbox Code Playgroud)
我的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;
Run Code Online (Sandbox Code Playgroud)
我认为您的错误是因为您尝试将HTML文件加载到通常接受javascript的文件列表中.我确实有一个解决方案.
在我开始之前,我有业力0.10.2,看起来你是在0.8.x或以下?我有这个在0.10.2工作,但我无法安装0.8.x. 我会尝试翻译0.8.x,但无法测试我在做什么,所以我将主要用0.10.x来描述.如果你有能力的话,可能更容易转移到最新的业力.
0.10.x
外部HTML部分可以由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
]
Run Code Online (Sandbox Code Playgroud)
这将允许您加载一个模块,module('inlinetemplates')该模块将插入主模板文件(而不是单个模板)的内容$templateCache.
0.8.4
因此,翻译为0.8.x ...我认为你需要使用html2js哪个不是那么强大,但是在这个版本中包含在业力中.您不需要在插件中安装或包含它,也无法配置它的使用方式,因此您只需要
preprocessors = { '**/*.html': ['html2js'] }
Run Code Online (Sandbox Code Playgroud)
创建的模块及其插入的项目$templateCache将使用您用于引用主模板html的路径命名.
0.10.x
现在,您应该能够加载相关模块并使用以下权限访问主模板文件的内容
var templates = $templateCache.get('inlinetemplates')
Run Code Online (Sandbox Code Playgroud)
剩下要做的就是将内联模板从主模板文件内容推送到$templateCache.这是使用angular script指令完成的,所以我们只需要编译/链接我们用angular加载的文件.你可以非常简单地做到这一点
$compile(templates)(scope);
Run Code Online (Sandbox Code Playgroud)
因此,将这些放在一起,您可以在describe需要加载模板的任何块中包含以下内容.
beforeEach(module('inlinetemplates'));
beforeEach(inject(function($compile, $templateCache, $rootScope) {
var templatesHTML = $templateCache.get('inlinetemplates');
$compile(templatesHTML)($rootScope);
}));
Run Code Online (Sandbox Code Playgroud)
0.8.4
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);
}));
Run Code Online (Sandbox Code Playgroud)
同样,我无法保证0.8.x指令可以正常工作,特别是没有调整,但这肯定适用于0.10.x.
Karma已经拥有将外部HTML部分推送到您的测试中的功能,所有缺少的功能都是能够正确地解释您的主模板.
| 归档时间: |
|
| 查看次数: |
2583 次 |
| 最近记录: |