【发布时间】:2018-08-08 19:12:31
【问题描述】:
我需要按照 Angular 组件 lifecycle 进行测试。我找不到制作这个的方法或工具。基本上想在给定的生命周期后调用测试。
以下示例:
my-component.ts:
@Component(...)
export class MyComponent implements OnInit, AfterViewChecked {
constructor(private element: ElementRef) {}
ngOnInit() {
this.element.innerHTML = 'Init';
}
ngAfterViewChecked() {
this.element.innerHTML = 'AfterViewChecked';
}
}
my-component.spec.ts
describe('MyComponent', () => {
let component: MyComponent;
let fixture: ComponentFixture<MyComponent>;
beforeEach(async(() => {
TestBed.configureTestingModule({
declarations: [MyComponent]
})
.compileComponents();
}));
beforeEach(() => {
fixture = TestBed.createComponent(MyComponent);
component = fixture.componentInstance;
fixture.detectChanges();
const compiled = fixture.debugElement.nativeElement;
});
// SHOULD BE CALLED AFTER ON INIT
it('should initiate', () => {
expect(compiled.textContent).toEqual('Init');
});
// SHOULD BE CALLED AFTER THE LIFECYCLE AfterViewChecked
it('should render view', () => {
expect(compiled.textContent).toEqual('ViewChecked');
});
});
【问题讨论】:
标签: javascript angular unit-testing testing