单元测试 – 使用构造函数参数测试Angular 2组件

前端之家收集整理的这篇文章主要介绍了单元测试 – 使用构造函数参数测试Angular 2组件前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
假设我有一个带有两个输入参数的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();
  });

createComponent调用不接受任何参数或允许我调用构造函数.如何为各种数值实例化/测试组件?

解决方法

正如JB Nizet指出的那样,当一个组件有@input参数时,你需要在beforeEach()中初始化它们:
“`

beforeEach(() => {
    fixture = TestBed.createComponent(SomeComponent);
    component = fixture.componentInstance;
    component.a = 1;
    component.b = 2;
    fixture.detectChanges();
});

“`

猜你在找的Angularjs相关文章