假设我有一个带有两个输入参数的Angular 2 Component:
@Component{... (omitted for clarity)} export class SomeComponent { @Input() a: number @Input() b: number }
当我想测试这个组件时,我有类似的东西:
beforeEach(async(() => { TestBed.configureTestingModule({ declarations: [ SomeComponent,],}) .compileComponents(); })); beforeEach(() => { fixture = TestBed.createComponent(SomeComponent); component = fixture.componentInstance; fixture.detectChanges(); });
解决方法
正如JB Nizet指出的那样,当一个组件有@input参数时,你需要在beforeEach()中初始化它们:
“`
“`
beforeEach(() => { fixture = TestBed.createComponent(SomeComponent); component = fixture.componentInstance; component.a = 1; component.b = 2; fixture.detectChanges(); });
“`