如何使用 jest 计算传递给函数的参数

问题描述 投票:0回答:1

我正在尝试做一个测试, 我想评估 getCall 函数接收 4 个参数, 但我不明白为什么我的测试不起作用。

import * as getCall from './index';

test('should be called with four aguments', () =>{
    const getCallSpy = jest.spyOn( getCall, 'getCall');

    const data = {   
        method: "GET",
        url: "myUrl",
        body: "myBody",
        headers: "myHeaders"
    }
    
    expect( getCallSpy ).toHaveBeenCalledWith( {
        data
    } );

});

index.js

    import * as http from 'http';
import * as https from 'https';

export const getCall = (data: {
      method: any;
      url: any;
      body: any;
      headers: any;
    }): any => {
  
  /*more code*/
 
  }
});
javascript typescript jestjs
1个回答
0
投票

您的

expect
说法是错误的。您“期望”使用像
getCallSpy
这样具有 4 个单独字段的对象来调用
data
。但是,您实际的
getCall
方法需要 4 个单独的参数,而不是 1 个参数。您需要将其更改如下:

expect(getCallSpy).toHaveBeenCalledWith(data.method, data.url, data.body, data.headers);
© www.soinside.com 2019 - 2024. All rights reserved.