【发布时间】:2021-12-16 23:23:50
【问题描述】:
我正在尝试使用 jasmine 为 ag-grid 编写单元测试。
-
HTML
<ag-grid-angular style="width: 500px; height: 400px;" class="ag-theme-alpine" [rowData]="rowData" [columnDefs]="columnDefs" rowSelection='single' (gridReady)="onGridReady($event)" > </ag-grid-angular> -
.ts
import { Component, Input, OnInit } from '@angular/core'; import { ColDef } from 'ag-grid-community'; import { DataGrid } from 'src/app/shared/models/datagrid.model'; @Component({ selector: 'app-simple-view', templateUrl: './simple-view.component.html', styleUrls: ['./simple-view.component.scss'] }) export class SimpleViewComponent implements OnInit { @Input('data') data: any | undefined; columnDefs: ColDef[] = [ { field: 'Geography', sortable: true }, { field: 'Category', sortable: true }, { field: 'DataType', sortable: true } ]; rowData: DataGrid[] | undefined; public gridApi: any; constructor() { } ngOnInit(): void { this.rowData = this.data; } onGridReady(params: any) { this.gridApi = params.api; } } -
spec.ts
it('grid API is available after `detectChanges`', () => { fixture.detectChanges(); expect(component.gridOptions.api).toBeTruthy(); });
此测试方法失败并出现以下错误
SimpleViewComponent > 网格 API 在detectChanges 之后可用
预期未定义是真实的。
错误:预期未定义是真实的。
在
在用户上下文。 (http://localhost:9876/karma_webpack/webpack:/src/app/features/overview/components/simple-view/simple-view.component.spec.ts:45:31)
在 Generator.next ()
在 asyncGeneratorStep (http://localhost:9876/karma_webpack/webpack:/node_modules/@babel/runtime/helpers/esm/asyncToGenerator.js:3:1)
【问题讨论】:
-
imports的imports数组中是否有AgGrid模块TestBed.configureTestingModule? -
是的,我已经导入了