Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/angular/26.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Angular 检查父组件中属性绑定的值';s检验_Angular - Fatal编程技术网

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');