簡體   English   中英

從 soap.createClientAsync 函數返回的方法的測試用例失敗

[英]Test case fail for method returning from soap.createClientAsync 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

在此處輸入圖像描述

soap.createClientAsync()的模擬解析值應該是一個肥皂客戶端。

例如

index.ts

import * as soap from 'soap';

interface ImportExperimentRequestDto {}
interface IffManPublishResponseDto {}

export class SoapHelper {
  public async registerInSoap(
    uploadRequest: ImportExperimentRequestDto,
  ): Promise<{ RegisterExperimentResult: IffManPublishResponseDto }> {
    const url = 'http://test.com/Services/Req.svc?wsdl';
    const client = await soap.createClientAsync(url);
    return client.RegisterExperimentAsync({ upload: uploadRequest });
  }
}

index.test.ts

import { Client } from 'soap';
import * as soap from 'soap';
import { SoapHelper } from '.';

describe('**** Register via soap service ****', () => {
  it('** should excecute register method **', async () => {
    const request = {};
    const mClient = ({
      RegisterExperimentAsync: jest.fn().mockResolvedValueOnce({ Result: 'mock result' }),
    } as unknown) as Client;
    jest.spyOn(soap, 'createClientAsync').mockResolvedValue(mClient);
    const soapHelper = new SoapHelper();
    const result = await soapHelper.registerInSoap(request);
    expect(result).toEqual({Result: 'mock result'});
  });
});

測試結果:

 PASS  stackoverflow/72604110/index.test.ts (9.87 s)
  **** Register via soap service ****
    ✓ ** should excecute register method ** (3 ms)

----------|---------|----------|---------|---------|-------------------
File      | % Stmts | % Branch | % Funcs | % Lines | Uncovered Line #s 
----------|---------|----------|---------|---------|-------------------
All files |     100 |      100 |     100 |     100 |                   
 index.ts |     100 |      100 |     100 |     100 |                   
----------|---------|----------|---------|---------|-------------------
Test Suites: 1 passed, 1 total
Tests:       1 passed, 1 total
Snapshots:   0 total
Time:        10.471 s

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM