我想测试一个函数,当有7个以上页面时,将一个类名添加到div容器中。但是不要让它起作用。
尝试了spyOn,但没有结果。
构造函数
constructor($document, $element) {
this.$document = $document;
this.$element = $element;
}
onInit函数
$onInit() {
const listPages = this.element.children;
const totalPages = listPages.length;
if (totalPages> 7) {
this.$document[0].getElementsByClassName('test-container')[0].className += ' test';
}
}
我尝试过:
beforeEach(() => {
this.documentSpy = jasmine.createSpyObj('$document', ['getElementsByClassName']);
this.elementSpy = jasmine.createSpyObj('$element', ['find']);
});
fit('should add class test when styles contain atleast 7 steps', () => {
let stepPage = new stepPageController(this.documentSpy, this.elementSpy);
stepPage.element = {
children: [
{ step: ['current'] },
{ step: ['step2'] },
{ step: ['step3'] },
{ step: ['step4'] },
{ step: ['step5'] },
{ step: ['step6'] },
{ step: ['step7'] },
{ step: ['step8'] }
]
};
const element = angular.element(this.$document[0].body).append('<div class="test-container"></div>');
spyOn(stepPage.$document[0], 'getElementsByClassName').and.returnValue(element);
// When
stepPage.$onInit();
// Then
expect(stepPage.$document[0].getElementsByClassName('test-container')[0].className).toBe('test');
});
但是将其作为错误:TypeError:无法读取未定义的属性“ 0”