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

时间:2017-03-07 07:50:32

标签: unit-testing angular testing typescript

假设我有一个带有两个输入参数的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调用不接受任何参数或允许我调用构造函数。如何针对各种数值实例化/测试组件?

1 个答案:

答案 0 :(得分:1)

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

TypeError: 'project' is an invalid keyword argument for Employee

```