例如假设我有以下
app.get('/', function(req, res) {
var ip;
if(req.headers['x-forwarded-for']){
ip = req.headers['x-forwarded-for'];
}
else {
ip = req.connection.remoteAddress;
}
});
我想进行单元测试以查看是否正确检索了 ip。一种方法如下
function getIp(req) {
var ip;
if(req.headers['x-forwarded-for']){
ip = req.headers['x-forwarded-for'];
}
else {
ip = req.connection.remoteAddress;
}
return ip;
}
app.get('/', function(req, res) {
var ip = getIp(req);
});
现在我有一个函数 getIp 可以进行单元测试。但是我仍然卡住了。如何将模拟的 req 对象输入 getIp?