【发布时间】:2020-09-22 18:52:40
【问题描述】:
我正在这里测试这个功能:
ngOnChanges(): void {
if (this.isCustomer) {
let sectionCopy = [...this.sections];
this.sectionsNotFilled = sectionCopy.find(section => section.filled === false);
...
}
}
我在很多资源上看到为了触发 ngOnChanges,我必须使用fixture.detectChanges();,但我仍然无法测试并且在我的测试中得到了错误的结果:
...
let component: XXX;
let fixture: ComponentFixture<XXX>;
...
const sections = [
{
key: 'SECTION1',
name: 'Section 1',
filled: true
},
{
key: 'SECTION2',
name: 'Section 2',
filled: false
}
];
...
it('should do test', () => {
const sectionsMock = {...sections};
component.sections = sectionsMock;
component.isCustomer = true;
console.log(component.sections); => section are here
//trigger
fixture.detectChanges();
console.log(component.sectionsNotFilled);
expect(component.sectionsNotFilled).toBe(true); => return false :/
});
有什么想法吗?我是不是做错了什么?
【问题讨论】:
标签: angular unit-testing jasmine onchange