我正在尝试测试一个模块中的函数。这个函数(我将它称为function_a)在同一文件中调用另一个不同的函数(function_b)。因此,这个模块的样子是这样的:
//the module file
module.exports.function_a = function (){
//does stuff
function_b()
};
module.exports.function_b = function_b = function () {
//more stuff
}
我需要用来自于function_b的特定结果测试function_a。
我想在我的测试文件中覆盖function_b,然后从我的测试文件中调用function_a,这样function_a将调用此覆盖函数而不是function_b。
请注意,我已经尝试并成功地覆盖了来自不同模块的函数,例如这个问题,但那不是我感兴趣的。
我尝试了下面的代码,据我所知,它不能工作。但它确实说明了我的意图。
//test file
that_module = require("that module")
that_module.function_b = function () { ...override ... }
that_module.function_a() //now uses the override function
有没有正确的方法来完成这个任务?