2017-04-05 107 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因为我如果这是有意或无意不知道。