关于ng-html2js插件有很多问题,但不幸的是,所有的答案都没有帮助我解决y问题。
我遵循了官方的安装指南和示例https://github.com/vojtajina/ng-directive-testing/blob/master/test/tabsSpec.js。
我的目标是测试一个具有templateUrl属性的指令,这是我的配置
karma.conf.js
files: [
... // lots of studd here
'app/views/**/*.html' // templates are all here
],
[...] // other karma configs here
preprocessors: {
'app/views/**/*.html': ['ng-html2js']
},我想测试的指令是非常基本的
'use strict';
angular.module('myAwesomeApp').directive('rzMenu', function () {
return {
templateUrl: 'views/directives/rzmenu.html',
restrict: 'E'
};
});的单元测试文件。
'use strict';
describe('Directive: rzmenu', function () {
// load the directive's module
beforeEach(module('myAwesomeApp'));
beforeEach(module('app/views/directives/rzmenu.html'));
var element,
scope;
beforeEach(inject(function ($rootScope, $compile) {
element = angular.element('<rzmenu></rzmenu>');
scope = $rootScope.$new();
element = $compile(element)(scope);
scope.$digest();
}));
it('should have content', inject(function () {
//scope.$digest();
var content = element.text();
expect(content).toBe('');
}));
});看看“应该有内容”的测试,难道不应该和模板一样吗?
为什么我得到和空的字符串?
提前感谢
发布于 2014-09-08 15:06:21
不过,我解决了两个错误:
1) cacheIds必须匹配templateUrl属性,因此
stripPrefix: 'app/'在配置中也需要调用模块,而不需要测试中的app/前缀。
beforeEach(module('views/directives/rzmenu.html'));2)指令名为camelCase,因此实例调用是错误的,下面是正确的
element = angular.element('<rz-menu></rz-menu>');:)
https://stackoverflow.com/questions/25725924
复制相似问题