Typescript 如何模拟在被测试服务使用的同一类中定义的函数的返回值

Typescript 如何模拟在被测试服务使用的同一类中定义的函数的返回值,typescript,unit-testing,jestjs,mocking,feathersjs,Typescript,Unit Testing,Jestjs,Mocking,Feathersjs,我正在为我的应用程序使用Feathers.js。我有一个eta服务,它使用eta类作为控制器。我的目标是测试服务,而不是类,特别是.find()方法.find()方法正在使用另一个私有方法getEta()。后者正在调用googleMapsClient.distancematrix() googleMapsClient被定义为类上的属性 导出类Eta实现部分{ 应用程序:应用程序; 选项:服务选项; 谷歌地图客户:任何; 位置之前的最大时间=90分钟; 构造函数(选项:ServiceOptions

我正在为我的应用程序使用Feathers.js。我有一个
eta
服务,它使用
eta
类作为控制器。我的目标是测试服务,而不是类,特别是
.find()
方法
.find()
方法正在使用另一个私有方法
getEta()
。后者正在调用
googleMapsClient.distancematrix()

googleMapsClient
被定义为类上的属性

导出类Eta实现部分{
应用程序:应用程序;
选项:服务选项;
谷歌地图客户:任何;
位置之前的最大时间=90分钟;
构造函数(选项:ServiceOptions={},应用程序:应用程序){
this.options=选项;
this.app=app;
this.googleMapsClient=新客户端({});
}
getStaleThreshold=():字符串=>{
返回力矩()
.减去(此.MAX\u TIME\u在\u位置\u过时\u分钟之前,'分钟')
.toISOString();
};
getEta=async(
纬度:数字,
经度:数字,
会议地点:{纬度:数字;经度:数字},
):承诺=>{
试一试{
const matrix=等待这个.googleMapsClient.distancematrix({
参数:{
起源:[{纬度,经度}],
目的地:[
{lat:meetupLocation.latitude,lng:meetupLocation.longitude},
],
模式:“驾驶”,
单位:'帝国',
出发时间:“现在”,
交通模型:“最佳猜测”,
关键字:process.env.GOOGLE_地图,
},
超时:3000,
});
常量{value}=matrix.data.rows[0]。元素[0]。持续时间;
//预计到达时间(秒)
返回值;
}捕获(错误){
抛出新的GeneralError(错误);
}
};
异步查找(params:params):承诺{
常量经度=params.query?params.query?经度:空;
常量纬度=params.query?params.query?纬度:空;
const type=params.query?params.query?type:null;
const connectionID=params.query?params.query?.connectionID:null;
const connectionService=this.app.service(“连接”);
const connection:any=wait connectionService.get(connectionID);
const{vehicleID,pickupLocation,dropoffLocation,dropoffTime,pickupTime,status}=连接;
let结果:LocationInformation |{}={};
开关(状态){
案件‘完成’:
if(类型===用户到用户的切换){
const etascess=等待此消息。getEta(
纬度,
经度,
拾取定位,
);
结果={
等一下,
纬度,
经度,
};
}
打破
违约:
打破
}
返回结果;
}
}
我想测试Eta查找服务方法,为此我需要模拟
getEta
。我不需要测试是否调用了
getEta
。我只需要模拟它的返回值并测试
.find()
方法

这就是我到目前为止所尝试的

从“../../src/services/Eta/Eta.class”导入{Eta};
描述(“eta”服务),()=>{
//…为brewety省略
它('正在获取驱动程序的ETA',async()=>{
常量数据={
经度:-122.434944,
纬度:37.7599487,
键入:“用户到用户切换”,
connectionID:confirmedConnection.id
};
const getEtaMock=jest.fn(异步()=>Promise.resolve(74));
const oldGetEta=Eta.prototype.getEta;
Eta.prototype.getEta=getEtaMock;
const etaData=等待应用程序服务('eta')。查找({
查询:{
…数据,
},
});
expect(etaData.toBeTruthy();
Eta.prototype.getEta=oldGetEta;
});
});

这似乎不起作用,并在连接get时生成
NotFound
,尽管它在没有模拟时确实获得了连接。有没有办法模拟
getEta
的返回值来测试
app.servcie('eta')。查找
call?

我也遇到了同样的问题。试试这个:

const app = require('./src/app'); // entry point to your feathers app

describe("'eta' service", () => {
 // ...omitted for brewety

 it('getting ETA for the driver', async () => {
    const data = { 
      longitude: -122.434944, 
      latitude: 37.7599487, 
      type: 'USER_TO_HANDOFF', 
      connectionID: confirmedConnection.id 
    };

    app.service('eta').getEta = jest.fn();
    app.service('eta').getEta.mockReturnValue(74);

    const etaData = await app.service('eta').find({
      query: {
        ...data,
      },
    });
    expect(etaData).toBeTruthy();

  });
});