Angular ag Grid:如何从单元测试访问gridApi?
我试图对一个函数进行单元测试,该函数包括对gridApi的调用,如Angular ag Grid:如何从单元测试访问gridApi?,angular,typescript,unit-testing,ag-grid,ag-grid-angular,Angular,Typescript,Unit Testing,Ag Grid,Ag Grid Angular,我试图对一个函数进行单元测试,该函数包括对gridApi的调用,如this.gridApi.getSelectedRows()。当单元测试到达此行时,它给出以下错误:失败:无法读取未定义的属性“getSelectedRows”,因为gridApi尚未定义 如何从单元测试中定义gridApi?它通常在onGridReady函数中定义如下: onGridReady(params) { this.gridApi = params.api; this.gridColumnApi =
this.gridApi.getSelectedRows()代码>。当单元测试到达此行时,它给出以下错误:失败:无法读取未定义的属性“getSelectedRows”,因为gridApi尚未定义
如何从单元测试中定义gridApi?它通常在onGridReady函数中定义如下:
onGridReady(params) {
this.gridApi = params.api;
this.gridColumnApi = params.columnApi;
this.gridApi.sizeColumnsToFit();
}
是否可以从单元测试触发onGridReady
?我不知道能派什么样的人去。是否有其他方法来定义this.gridApi?this.gridApi和this.gridColumnApi在测试用例中自动初始化。为此,需要从“ag网格角度”导入AgGridModule。
并初始化组件,如下代码所示:
let component: MyComponent;
let fixture: ComponentFixture<MyComponent>;
beforeEach(async(() => {
TestBed.configureTestingModule({
declarations: [MyComponent],
imports: [
AgGridModule.withComponents([])
]
}).compileComponents();
fixture = TestBed.createComponent(MyComponent);
component = fixture.componentInstance;
fixture.detectChanges();
}));
并声明一个变量
gridOptions: GridOptions = <GridOptions>{};
您是否遵循了这些步骤?如果您可以使用@Goodsmaritan Yes之类的内容发布测试或以其他方式显示测试的非工作示例,这会很有帮助,但我仍然看到相同的错误。@Matt123您找到了测试的解决方案吗it@Nidhinkumar不,不幸的是我想不出来。
gridOptions: GridOptions = <GridOptions>{};
it('onGridReady called', () => {
const data = {
api: component.gridOptions.api,
columnApi: component.gridOptions.columnApi
}
component.onGridReady(params);
expect(component.gridApi).not.toBeNull();
expect(component.gridColumnApi).not.toBeNull();
});