使用jasmine测试PrimeNg数据表



我们如何使用jasmine 在angular中编写针对priming filter函数的单元测试

home.component.html

<input type="text" pInputText size="50" placeholder="Global Filter" (input)="filterGlobal(dt, $event)" >

home.component.ts

filterGlobal(dt: any, event){
dt.filterGlobal(event.target.value, 'contains')
}

home.component.spec.ts

回答这个问题是因为我花了一段时间才弄清楚。

您需要在单元测试中使用fakeAsync((和tick((,以便有时间更新所有内容。使用TestBed.configureTestingModule((和TestBed.createComponent((创建夹具。然后

it('name of test', fakeAsync(() => {
//Arrange
const mockInputEvent = { target: { value: 'Mock Value' } };
//Act
fixture.componentInstance.filterGlobal(mockInputEvent);
tick(3000);
fixture.detectChanges();

//Assert
//Write your assertions here
}));

最新更新