我想在mail.service.tsgetAllGroups()中為我的方法撰寫一個單元測驗:
public async getAllGroup(): Promise<{ id: number, name: string }[]> {
try {
return (await lastValueFrom(this.groupService.GetAllGroup({}))).groups;
} catch (error) {
throw error;
}
}
問題是我想模擬GetAllGroups()一個 grpc 方法并獲取資料。我如何在mail.service.ts添加 groupService :
constructor(
@Inject('groupService') private groupClient: ClientGrpc,
) { }
private readonly groupService = this.groupClient.getService<GroupService>('groupService');
在mail.service.spec.ts我如何提供 groupService:
{
provide: 'groupService',
useValue: createMock<ClientGrpc>()
.getService<GroupService>('groupService')
}
我為getAllGroups()方法撰寫的測驗:
it(`getAllGroup() should return list of gropus`, async () => {
const groupMock = createMock<MailService>();
groupMock.GetAllGroup.mockReturnValue( of({ groups: [{ id: 123, name: "abc" }] }));
expect(service.getAllGroup()).toEqual([
{ id: 123, name: "abc" }
]);
})
測驗執行后失敗并回傳:
Expected: [{"id": 123, "name": "abc"}]
Received: {}
56 | groupMock.GetAllGroup.mockReturnValue( of({ groups: [{ id: 123, name: "abc" }] }));
57 |
> 58 | expect(service.getAllGroup()).toEqual([
| ^
59 | { id: 123, name: "abc" }
60 | ]);
61 | })
uj5u.com熱心網友回復:
我發現了問題......我應該在之前使用 awaitservice.getAllGroup()像這樣:
expect(await service.getAllGroup()).toEqual([
{ id: 123, name: "abc" }
]);
轉載請註明出處,本文鏈接:https://www.uj5u.com/gongcheng/477581.html
