簡體   English   中英

使用構造函數參數測試Angular 2組件

[英]Test Angular 2 Component with Constructor Parameter

假設我有一個帶有兩個輸入參數的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();
});

```

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM