我刚开始使用 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);
}
}
Run Code Online (Sandbox Code Playgroud)
我正在尝试模拟“getData”,以便我可以为“logData”编写单元测试。我需要模拟整个“got”模块吗?谢谢。