关于 ng-html2js 插件有很多问题,但不幸的是所有答案都没有帮助我解决问题。
我按照官方安装指南和示例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('');
}));
});
看看“应该有内容”测试,内容不应该和模板一样吗?
为什么我得到空字符串?
提前致谢
最佳答案
没关系, 我解决了 2 个错误:
1)cacheIds 必须匹配 templateUrl 属性,所以
stripPrefix: 'app/'
在配置中需要,并且在测试中调用不带 app/前缀的模块。
beforeEach(module('views/directives/rzmenu.html'));
2) 指令名称是驼峰命名法,因此实例调用是错误的,下面是正确的
element = angular.element('<rz-menu></rz-menu>');
:)
关于javascript - 使用 ng-html2js 进行 AngularJS 指令测试,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25725924/