2017-09-21 36 views
1

我对单元测试相当陌生。我想知道我是否可以测试一个智能组件是否会将一些作为@Input()传递给愚蠢组件(使用Angular 4+)。测试一个组件是否将一个@Input传递给另一个

起初,我想过检查,如果该属性存在:

it('should have some data', async(() => { 
    expect(component.data).toBeTruthy(); 
})); 

不过,我面临两个问题:1),它告诉我,如果data是真实的,但并不一定意味着它是被作为一个传递输入到我的愚蠢组件; 2)如果data属性不存在,那么测试套件将不会被执行。

任何提示?有没有更好的方法来解决这个问题?谢谢。

回答

1

由于输入绑定作为变更检测的一部分进行处理,因此您可以基本更改父组件上绑定所使用的属性,然后在父组件上运行detectChanges()并检查子组件中的输入属性是否已更改。沿着这些路线的东西:

parentComponent = TestBed.createComponent(BannerComponent); 
const childComponentEl = fixture.debugElement.query(By.directive(ChildComponent)); 
const childComponent = childComponentEl.injector.get(ChildComponent); 

it('should have some data', async(() => { 
    parentComponent.componentInstance.boundProperty = 3; 
    parentComponent.detectChanges(); 
    expect(childComponent.inputProperty).toBe(3); 
})); 

你可以阅读更多关于为什么输入绑定在更新:

相关问题