使用 mocha+sinon 测试 ExpressJS 路由时,如何 "stub" 路由本地的函数?

When testing an ExpressJS route using mocha+sinon, how do you "stub" a function that's local to the route?

所以在一个文件中,我定义了这条路线:

router.post('/', security.authenticate, function(req, res, next) {
    //Doing prep stuff

    //Do database work (<< this is what im really testing for)

    //Calling another work function.
    createFormUser(req.body, (ret) => {
        return res.json(ret.createdUser);
    });

});

后接此函数:

var createFormUser = (ourUser, call) => {
    // does a bunch of misc work and creation for another database
    // unrelated to current tests.
}

我想测试这条路线。通常,我会创建一个数据库的沙箱实例,这样它就可以做任何它想做的事情,向测试中的路由发出一个 http 请求,最后在那个 http 调用的 return 中执行 expects() 。

但是,我不希望调用 "createFormUser" 函数,因为 1) 它做了一些花哨的事情,对于这个测试来说真的很难包含 2) 我将在别处测试它。

在正常测试中,此时我会使用 sinon 来存根该函数。但在这种情况下,我实际上没有 object 引用,因为这都是通过对服务器的 HTTP 请求完成的,mocha 在测试时假脱机。

所以我的问题和标题一样,stub/replace/ignore/etc这个方法怎么才能在测试的时候不被调用呢?

如@DavidKnipe 所述,我所要做的就是通过以下方式导出方法:

module.exports.createFormUser = (ourUser, call) => { ... }

并且能够单独测试该方法并通过 sinon.stub 阻止其执行。