Angularjs 角度测试指令-find没有按预期工作(或者根本没有工作?)
我有指示。它获取一个数组并创建一个堆叠条。虽然指令运行良好,但单元测试失败得很惨。我试过:Angularjs 角度测试指令-find没有按预期工作(或者根本没有工作?),angularjs,angularjs-directive,chai,Angularjs,Angularjs Directive,Chai,我有指示。它获取一个数组并创建一个堆叠条。虽然指令运行良好,但单元测试失败得很惨。我试过: describe('Stacked bar directive', function(){ var scope, elem; beforeEach(inject(function(_$compile_, _$rootScope_){ // The injector unwraps the underscores (_) from around the par
describe('Stacked bar directive', function(){
var scope, elem;
beforeEach(inject(function(_$compile_, _$rootScope_){
// The injector unwraps the underscores (_) from around the parameter names when matching
$compile = _$compile_;
$rootScope = _$rootScope_;
}))
beforeEach(function(){
scope = $rootScope.$new();
scope.distribution = [[0.4,'diamonds'],
[0.05,'gold'],
[0.15,'silver'],
[0.4, 'bronze']];
elem = angular.element('<bars-chart chart-data="distribution" chart-width="200"></bars-chart>');
$compile(elem)(scope);
scope.$digest();
});
it('Should display the correct number of bars', function(){
dump(elem)
expect(elem.find(".bars").length).to.equal(4)
//and no other find works either..
});
由于编译的指令到处都有.bar类,所以这没有意义
我注意到,转储elem不会显示呈现的指令,但是:
{0:,长度:1}
因此,可能编译指令的某些东西对我不起作用
我正在使用它来简化dom/css的测试,但是它对这个问题没有任何影响,至少我可以告诉你,我禁用了它,得到了同样的效果
有什么想法吗?我很乐意为您提供帮助我在以下地方找到了这张小纸条: 查找-仅限于按标记名查找
可能是您的问题的原因…这是您的整个测试吗?我没有看到任何加载包含指令的模块的调用。这意味着您的指令根本没有被注册,并且在编译时不会被调用 通常,您应该执行以下操作:
beforeEach(module('myApp'))
这将确保加载“myApp”模块,并且在$compile时该模块中的指令可用。足够正确,但不是问题所在,首先是因为我在测试中包括jquery,然后使用jquery或直接使用jquery。也因为这样我至少可以做``expectelem.text.to.contain'gold'并得到一些东西。
beforeEach(module('myApp'))