【发布时间】:2017-03-03 04:29:23
【问题描述】:
如何在 Angular 2.0.0 Final Release 中模拟组件?我在模拟具有我在另一个组件中用于逻辑的变量的组件时遇到问题。具体来说,我想模拟 PrimeNG Datatable 选择。
下面的示例代码。
table.component.html
<p-dataTable
#table
[value]="myDatasource"
[(selection)]="mySelections"
...
>
table.component.ts
@Component({
selector: 'my-table',
templateUrl: './table.component.html'
})
export class TableComponent{
@ViewChild('table') datatable;
my-component.component.html
<my-table #mytable></my-table>
my-component.component.ts
@Component({
selector: 'my-component',
templateUrl: './my-component.component.html'
})
export class MyComponent {
@ViewChild('#mytable') mytable;
myFunction() : void {
if(this.mytable.table.selection.length === 0){
console.log();
} else{
console.log();
}
}
如何模拟它,以便我可以为 table.component.ts 中的 selection 设置值以在 table.component.ts 中进行测试strong>my-component.component.ts?
【问题讨论】:
标签: unit-testing angular mocking karma-jasmine primeng