Javascript 如何模拟一个回调函数来测试另一个函数的参数

Javascript 如何模拟一个回调函数来测试另一个函数的参数,javascript,node.js,unit-testing,mocha.js,chai,Javascript,Node.js,Unit Testing,Mocha.js,Chai,我的要求是模拟回调函数,它位于另一个函数的参数内,用于单元测试 var jwt = require("jsonwebtoken"); var APICall = require('./requestFile'); function Helper(){ this.request = new APICall(); } Helper.prototype.getData = function (done) { var headers = { 'content-t

我的要求是模拟回调函数,它位于另一个函数的参数内,用于单元测试

 var jwt = require("jsonwebtoken");
 var APICall = require('./requestFile');

 function Helper(){
 this.request = new APICall();
  }

 Helper.prototype.getData = function (done) {
    var headers = {
        'content-type': "application/json",
    };
    var _self = this;
    this.request.get(this.pub_URL, get_headers, function (err, res, body) {
        if (!err && res.statusCode === 200) {
            console.log("Got data: ", body);
            done(null, body);
        }
        else {
            console.log("Error occured while fetching data: " + err)
            done(err, null);
        }
    });
  }
 }
我想模拟this.request.get()作为参数调用的回调函数,以便我的测试能够覆盖else块 log(“获取数据时出错:+err”)

这是我的测试文件和代码库

const Helper=require('../Helper');
var APICall=require('../requestFile');
设hlp=newhelper();
描述('APP',函数(){
在(函数()之前){
设res={
状态代码:500
}
让错误={
消息:“存在错误”
};
var get_头={
“内容类型”:“应用程序/json”,
};
sinon.stub(APICall.prototype,'get').callsFake(函数(完成){
完成(错误,恢复)
})
})
在(函数()之后{
APIRequester.prototype.get.restore();
});
它('在调用请求时应告知错误',函数(完成){
获取数据(函数(错误,数据){
expect(data).to.be.a('string'))
完成()
})
})
})

以下是单元测试解决方案:

helper.js

const APICall=require(“./request”);
函数助手(){
this.request=new APICall();
this.pub_URL=”https://github.com";
}
Helper.prototype.getData=函数(完成){
常量头={
“内容类型”:“应用程序/json”,
};
this.request.get(this.pub_URL、头、函数(err、res、body){
如果(!err&&res.statusCode==200){
log(“获取数据:”,正文);
完成(空,正文);
}否则{
console.log(“获取数据时出错:+err”);
完成(错误,空);
}
});
};
module.exports=Helper;
request.js

函数APICall(){}
APICall.prototype.get=函数(url、头、回调){};
module.exports=APICall;
helper.test.js

const APICall=require(“./request”);
const sinon=要求(“sinon”);
const Helper=需要(“./Helper”);
描述(“56827977”,()=>{
之后(()=>{
sinon.restore();
});
它(“应该正确获取数据”,()=>{
const helper=new helper();
常量mRes={statusCode:200};
const mBody=“假数据”;
sinon.存根(APICall.prototype,“get”).收益率(空、mRes、mBody);
const callback=sinon.stub();
getData(回调);
sinon.assert.calledWithJustice(
APICall.prototype.get,
"https://github.com",
{
“内容类型”:“应用程序/json”,
},
sinon.match.func,
);
sinon.assert.calledWithJustice(回调,null,“假数据”);
});
它(“应该处理错误”,()=>{
const helper=new helper();
const mError=新错误(“网络错误”);
sinon.stub(APICall.prototype,“get”).收益率(mError,null,null);
const callback=sinon.stub();
getData(回调);
sinon.assert.calledWithJustice(
APICall.prototype.get,
"https://github.com",
{
“内容类型”:“应用程序/json”,
},
sinon.match.func,
);
sinon.assert.calledWithJustice(callback,mError,null);
});
});
单元测试结果和覆盖率报告:

56827977
有数据:假数据
✓ 应该正确获取数据
提取数据时出错:错误:网络错误
✓ 应该处理错误
2次通过(10毫秒)
----------------|----------|----------|----------|----------|-------------------|
文件|%Stmts |%Branch |%Funcs |%Line |未覆盖行|s|
----------------|----------|----------|----------|----------|-------------------|
所有文件| 100 | 100 | 88.89 | 100 ||
helper.js | 100 | 100 | 100 | 100 ||
helper.test.js | 100 | 100 | 100 | 100 ||
request.js | 100 | 100 | 50 | 100 ||
----------------|----------|----------|----------|----------|-------------------|

源代码:

以下是单元测试解决方案:

helper.js

const APICall=require(“./request”);
函数助手(){
this.request=new APICall();
this.pub_URL=”https://github.com";
}
Helper.prototype.getData=函数(完成){
常量头={
“内容类型”:“应用程序/json”,
};
this.request.get(this.pub_URL、头、函数(err、res、body){
如果(!err&&res.statusCode==200){
log(“获取数据:”,正文);
完成(空,正文);
}否则{
console.log(“获取数据时出错:+err”);
完成(错误,空);
}
});
};
module.exports=Helper;
request.js

函数APICall(){}
APICall.prototype.get=函数(url、头、回调){};
module.exports=APICall;
helper.test.js

const APICall=require(“./request”);
const sinon=要求(“sinon”);
const Helper=需要(“./Helper”);
描述(“56827977”,()=>{
之后(()=>{
sinon.restore();
});
它(“应该正确获取数据”,()=>{
const helper=new helper();
常量mRes={statusCode:200};
const mBody=“假数据”;
sinon.存根(APICall.prototype,“get”).收益率(空、mRes、mBody);
const callback=sinon.stub();
getData(回调);
sinon.assert.calledWithJustice(
APICall.prototype.get,
"https://github.com",
{
“内容类型”:“应用程序/json”,
},
sinon.match.func,
);
sinon.assert.calledWithJustice(回调,null,“假数据”);
});
它(“应该处理错误”,()=>{
const helper=new helper();
const mError=新错误(“网络错误”);
sinon.stub(APICall.prototype,“get”).收益率(mError,null,null);
const callback=sinon.stub();
getData(回调);
sinon.assert.calledWithJustice(
APICall.prototype.get,
"https://gi