[英]Jest Unit Testing fails in soap.createClientAsync returning function
我有一個肥皂 RestAPI 來調用從我使用帶有異步等待的肥皂庫的服務中執行一個函數。 代碼工作正常。 當進行單元測試時,測試用例在客戶端返回的回調方法處失敗。 代碼和 UT 錯誤如下。
調用 Soap 客戶端的函數 - 代碼 - Helper.ts
class soapHelper {
public async registerInSoap(
uploadRequest: ImportExperimentRequestDto,
): Promise<{ RegisterExperimentResult: IffManPublishResponseDto }> {
const url = "http://test.com/Services/Req.svc?wsdl";
const client = await soap.createClientAsync(flavourUrl);
return client.RegisterExperimentAsync({ upload: uploadRequest});
}
}
測試用例 - 代碼
describe("**** Register via soap service ****", () => {
it("** should excecute register method **", async () => {
const request = cloneDeep(MOCK.API_PAYLOAD);
const clientResponse = {
RegisterExperimentAsync: jest.fn(),
};
jest.spyOn<any, any>(soap, "createClientAsync").mockReturnValueOnce(() => Promise.resolve(clientResponse));
const result= await soapHelper.registerInSoap(request);
expect(result).toEqual({ Result: AFB_MOCK_RESPONSE.API_RESPONSE });
});
});
錯誤
TypeError: client.RegisterExperimentAsync is not a function
您告訴 Jest soapHelper.createClientAsync
應該返回一個具有RegisterExperimentAsync
方法的函數,而不是告訴它應該立即使用RegisterExperimentAsync
方法返回對象的承諾。 這個片段應該修復它:
describe("**** Register via soap service ****", () => {
it("** should excecute register method **", async () => {
const request = cloneDeep(MOCK.API_PAYLOAD);
const clientResponse = {
RegisterExperimentAsync: jest.fn(),
};
jest.spyOn<any, any>(soap, "createClientAsync").mockResolvedValue(clientResponse);
const result= await soapHelper.registerInSoap(request);
expect(result).toEqual({ Result: AFB_MOCK_RESPONSE.API_RESPONSE });
});
});
現在soapHelper.createClientAsync
正確地返回了一個要await
的承諾,並且承諾的解析值有一個RegisterExperimentAsync
方法
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.