[英]Async await function is not getting called using jest
I am new to jest and i am trying to call await function it return the promise.我是开玩笑的新手,我正在尝试调用 await 函数,它返回承诺。 But i am getting and error like expected calls 1 and received calls is 0但我收到错误,如预期的电话 1 和收到的电话是 0
Code:代码:
public async callDataSourceCommand(dialogData: any, RecipeId: string) {
const gridItems = await this.dataSourceService.myPromiseMethod(id, collection);
}
MockData模拟数据
public get dataSourceServiceMock(): any = {
return {
myPromiseMethod: function () {
return Promise.resolve({
id: '123',
collection: []
});
}
}
}
Test suite测试套件
it('1. Should execute ', async() => {
const myDialogApp: DialogApp = TestBed.get(DialogApp);
myDialogApp.selectedOrder = selectedOrder;
myDialogApp.RecipeId = Recipe.__id;
myDialogApp.callDataSourceCommand(dialogData, RecipeId);
jest.spyOn(dataSourceServiceMock, 'myPromiseMethod');
expect(dataSourceServiceMock.myPromiseMethod).toHaveBeenCalled();
});
After adding shuan's comment, still i am facing an issue is like,添加 shuan 的评论后,我仍然面临一个问题,就像,
console.error node_modules/zone.js/dist/zone.js:703 Unhandled Promise rejection: Unexpected token o in JSON at position 1 ; console.error node_modules/zone.js/dist/zone.js:703 未处理的承诺拒绝:JSON 中的意外令牌 o 在位置 1 ; Zone: ProxyZone ;区域: ProxyZone ; Task: Promise.then ;任务: Promise.then ; Value: SyntaxError: Unexpected token o in JSON at position 1 at JSON.parse () at OrderManagementMultipleBatchesDialogApp.值:SyntaxError:OrderManagementMultipleBatchesDialogApp 中 JSON.parse () 位置 1 处的 JSON 中的意外标记 o。 (D:\\DCS_WorkSpace\\src\\DCSPlus\\UI\\libs\\order-management\\apps\\src\\components\\order-management-multiple-batches-dialog-app\\order-management-multiple-ba tches-dialog-app.factory.ts:102:30) (D:\\DCS_WorkSpace\\src\\DCSPlus\\UI\\libs\\order-management\\apps\\src\\components\\order-management-multiple-batches-dialog-app\\order-management-multiple-ba tches-dialog-app.factory .ts:102:30)
i have updated the test case我已经更新了测试用例
MockData模拟数据
public get dataSourceServiceMock(): any = {
return {
myPromiseMethod: function () {
return Promise.resolve({
selectedOrder: {
earlierStartTime: '2/5/2020',
__id: 'orderId123'
},
batchCollection: {
__id: 'b1order 1',
masterRecipeName: 'New recipe_V1.0',
plannedQuantity: '3',
masterRecipeId: 'ns=6;s=4/ProjectData/1',
actualQuantity: '1',
description: 'batchDesc',
}
});
}
}
}
Test suite测试套件
it('1. Should execute ', async() => {
const myDialogApp: DialogApp = TestBed.get(DialogApp);
myDialogApp.selectedOrder = selectedOrder;
myDialogApp.RecipeId = Recipe.__id;
jest.spyOn(dataSourceServiceMock, 'myPromiseMethod');
await myDialogApp.callDataSourceCommand(multipleBatchData, masterRecipeId);
expect(dataSourceServiceMock.myPromiseMethod).toHaveBeenCalled();
});
As Jaromanda correctly wrote, you need to await
the asynchronous method.正如 Jaromanda 正确写的,您需要await
异步方法。 Also, you need to spy on the method before acting not after acting.此外,您需要在行动之前而不是在行动之后窥探方法。
Here is a simplified, standalone version of your original example that you can run in Jest.这是您可以在 Jest 中运行的原始示例的简化、独立版本。
class MyDialogApp {
constructor(private dataSourceService: any) {}
public async callDataSourceCommand() {
await this.dataSourceService.myPromiseMethod();
}
}
const dataSourceServiceMock = {
myPromiseMethod: function() {
return Promise.resolve({
id: '123',
collection: []
});
}
};
const myDialogApp = new MyDialogApp(dataSourceServiceMock);
it('1. Should execute ', async () => {
// arrange
jest.spyOn(dataSourceServiceMock, 'myPromiseMethod');
// act
await myDialogApp.callDataSourceCommand();
// assert
expect(dataSourceServiceMock.myPromiseMethod).toHaveBeenCalled();
});
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.