gpt4 book ai didi

angular - 单元测试 - Angular 9 中未定义的 ag-grid API onFilterChanged

转载 作者:行者123 更新时间:2023-12-04 13:52:04 25 4
gpt4 key购买 nike

我正在为 Angular 中的 Ag-grid 编写单元测试用例,其中我有 Angular Grid: External Filter 这是切换过滤器复选框。
我收到 “类型错误:无法读取未定义的属性“onFilterChanged””
我正在测试这个方法:

toggleCheckboxMethod({ checked }): void {
isChecked = checked;
this.gridApi.onFilterChanged(); //when this method initiates it causes for test to fail
}
 beforeEach(async(() => {
TestBed.configureTestingModule({
imports: [
RouterTestingModule.withRoutes([]),
HttpClientModule,
HttpClientTestingModule,
AgGridModule.withComponents([]),
MatDialogModule,
BrowserAnimationsModule
],
declarations: [ TestComponent ],
providers: []

})
.compileComponents();
}));

beforeEach(() => {
fixture = TestBed.createComponent(TestComponent);
component = fixture.componentInstance;
fixture.detectChanges();
});

it('should toggle checkbox', () => {
let isChecked = false;
spyOn(component, 'toggleCheckboxMethod').and.callThrough();
component.toggleCheckboxMethod({ checked: true });
expect(component.toggleCheckboxMethod).toHaveBeenCalled();
expect(isChecked).toEqual(true);
});

最佳答案

我想 gridApi在您断言的那一刻未定义。测试 ag-grid在断言之前必须等待其异步任务完成可能会很奇怪。
我会做这样的事情:
创建一个像这样的实用程序函数,您可以在继续之前等待某事为真:

import { interval } from 'rxjs';
.....
export const waitUntil = async (untilTruthy: Function): Promise<boolean> => {
while (!untilTruthy()) {
await interval(25).pipe(take(1)).toPromise();
}
return Promise.resolve(true);
};
it('should toggle checkbox', async (done: DoneFn) => {
let isChecked = false;
spyOn(component, 'toggleCheckboxMethod').and.callThrough();
// wait until component.gridApi is truthy before carrying forward
await waitUntil(() => !!component.gridApi);
component.toggleCheckboxMethod({ checked: true });
expect(component.toggleCheckboxMethod).toHaveBeenCalled();
expect(isChecked).toEqual(true);
done(); // call done to let Jasmine know you're done (this could be optional)
});

This是另一个与您有相同问题的人,如果您不喜欢我的问题,您可能需要查看答案的解决方案。

关于angular - 单元测试 - Angular 9 中未定义的 ag-grid API onFilterChanged,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/68318114/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com