Javascript 使用ng-html2js的AngularJS指令测试
关于ng-html2js插件有很多问题,但不幸的是,所有的答案都不能帮助我解决这个问题 我遵循了官方的安装指南和示例 我的目标是测试具有templateUrl属性的指令,这是我的配置 karma.conf.jsJavascript 使用ng-html2js的AngularJS指令测试,javascript,angularjs,unit-testing,ng-html2js,Javascript,Angularjs,Unit Testing,Ng Html2js,关于ng-html2js插件有很多问题,但不幸的是,所有的答案都不能帮助我解决这个问题 我遵循了官方的安装指南和示例 我的目标是测试具有templateUrl属性的指令,这是我的配置 karma.conf.js files: [ ... // lots of studd here 'app/views/**/*.html' // templates are all here ], [...] // other karma configs here preprocessors: {
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('');
}));
});
“严格使用”;
描述('指令:rzmenu',函数(){
//加载指令的模块
在每个模块之前(模块('myAwesomeApp');
在每个模块之前(模块('app/views/directives/rzmenu.html');
var元素,
范围
beforeach(注入(函数($rootScope,$compile){
元素=角度。元素(“”);
scope=$rootScope.$new();
元素=$compile(元素)(范围);
范围。$digest();
}));
它('should have content',inject(function)(){
//范围。$digest();
var content=element.text();
expect(content.toBe)('');
}));
});
看看“应该有内容”测试,内容不应该与模板相同吗
为什么我得到一个空字符串
提前谢谢不用客气,
我解决了两个错误:
1) CacheID必须与templateUrl属性匹配,因此
stripPrefix: 'app/'
在配置中需要,以及在测试中调用不带app/前缀的模块
beforeEach(module('views/directives/rzmenu.html'));
2) 指令名是camelCase,因此实例调用是错误的,下面是正确的
element = angular.element('<rz-menu></rz-menu>');
element=angular.element(“”);
:)对于一些假设可以解决很多测试难题的东西,它似乎只需要设置就可以解决很多问题!感谢您提供的信息,如有疑问,请添加条带前缀:“app/”