我是初次使用Jest进行单元测试。如何模拟这个简单的HTTP请求方法“getData”?以下是类代码:
const got = require("got")
class Checker {
constructor() {
this.url
this.logData = this.logData.bind(this);
this.getData = this.getData.bind(this);
}
async getData(url) {
const response = await got(url);
const data = await response.body;
return data;
}
async logData(first, second, threshold) {
let data = await this.getData(this.url)
console.log("received " + data.body);
}
}
我想模拟“getData”,以便为“logData”编写单元测试。我需要将整个“got”模块都模拟吗?谢谢。