Angular 检查父组件中属性绑定的值';s检验
我正在为包含名为Angular 检查父组件中属性绑定的值';s检验,angular,Angular,我正在为包含名为的子组件的父组件编写测试规范。如何测试config属性是否已分配myConfigX对象,如下所示: <album-art [config]="myConfigX"></album-art> 我所能得到的最接近的方法是创建子组件的超轻量级模拟,选择其DebugElement,并对其componentInstance进行测试,如下所示: 模拟子组件(将其添加到测试模块的声明中): 父组件模板: ... <album-art [config]="myCo
的子组件的父组件编写测试规范。如何测试config
属性是否已分配myConfigX
对象,如下所示:
<album-art [config]="myConfigX"></album-art>
我所能得到的最接近的方法是创建子组件的超轻量级模拟,选择其
DebugElement
,并对其componentInstance
进行测试,如下所示:
模拟子组件(将其添加到测试模块的声明中):
父组件模板:
...
<album-art [config]="myConfigX"></album-art>
...
这比在模拟模板中测试插值要好,因为在本例中,您可以检查实际对象引用的相等性
遗憾的是,您无法访问fixture.debugElement.query(By.css('album-art')).inputs.config之类的内容,在这种情况下,您就不需要mock组件了
expect(fixture.nativeElement.querySelector('album-art').getAttribute('ng-reflect-config'));
@Component({
selector: 'album-art',
template: '',
})
class MockAlbumArtComponent {
@Input()
config: AlbumArtConfig;
}
...
<album-art [config]="myConfigX"></album-art>
...
expect(fixture.debugElement.query(By.css('album-art')).componentInstance.config).toBe(instance.myConfigX, 'correct config object passed');