2017-04-05 72 views
0

我正在將Angular 2應用程序升級到V4.0.1,並且遇到了一些測試問題。Angular 4單元測試子組件的屬性

我試圖獲取子組件的屬性並測試給出的值是否正確。

不幸的是,即使組件在JiT構建和AoT構建中正確運行,這些屬性在測試環境中似乎仍未定義。

<div> 
    <div *ngIf="isComponentEnabled"> 
    <child-component 
     superBaseUrl="super_url" i18n-superBaseUrl="frk|" 
     [slug]="slug" 
     [name]="name" 
     [age]="age"> 
    </child-component> 
    ..... 

    </div> 
</div> 

試驗,打破:

 it(`GIVEN parent-component 
     THEN it should have correct attribute values`,() => { 
     componentInstance.name = 'Bar'; 
     componentInstance.age = 90; 
     componentInstance.slug = 'foo'; 

     attributes = specService.queryAttributes(
     componentFixture, 'child-component'); 

     console.log('HEYA -> ', specService.queryDebugElement(
     componentFixture, 'child-component')); 
     expect(attributes['superbaseurl']).toBe('super_url'); 
     expect(attributes['i18n-superbaseurl']).toBe('frk|'); 
     expect(attributes['ng-reflect-slug']).toBe('foo'); 
     expect(attributes['ng-reflect-name']).toBe('Bar'); 
     expect(attributes['ng-reflect-age']).toBe('90'); 
    }); 

斷試驗的輸出繼電器:

測試只更新到我的父組件的角4.0.1

模板爆發後

Expected undefined to be 'foo'. 
Expected undefined to be 'Bar'. 
Expected undefined to be '90'. 

控制檯日誌測試裏面:

<child-component _ngcontent-c115="" i18n-superbaseurl="frk|" superbaseurl="super_url"> 
</child-component> 

,所以我不明白爲什麼在4版本的屬性不會在這種情況下產生的。在2.X.X版本中,我們有ng-reflection,但似乎不再是這種情況。

我也試着在模板中添加一個基本的屬性(toto =「toto」),並且這個在測試的console.log中出現。

有沒有人有一個想法,爲什麼屬性不再顯示?

P.S:specService.queryAttributes只是在查詢屬性之前調用detectChanges的包裝器。

回答

0

找到了!

對於任何感興趣的人,如果您希望屬性是可查詢的,您必須添加每個子組件,並在版本4.0.1中將它們編譯到TestBed中。

在版本2.X中,這不是必需的。

編輯

而且,看,https://github.com/angular/angular/issues/15822因爲我如果這是有意或無意不知道。