当使用mocha + sinontestingExpressJS路线时,如何“存根”一个本地path的函数?

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

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); }); }); 

其次是这个function:

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

我想testing这条路线。 通常情况下,我只是创build一个数据库的沙箱实例,以便它可以做任何想要的事情,在testing中对路由发出http请求,最后在http呼叫的返回中做预期()。

不过,我不希望“createFormUser”函​​数被调用,因为1)它做了一些真正难以容纳的花式狗屎这个testing2)我将在别处testing它。

在一个正常的testing中,我会在这一点上用sinon来存储函数。 但在这种情况下,我实际上并没有一个对象引用,因为这一切都通过HTTP请求到服务器完成testing时mocha后退。

所以我的问题是一样的标题,如何可以存根/replace/忽略/等这个方法,所以它不被调用在testing过程中?

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

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

并能够单独testing该方法,并通过sinon.stub阻止其执行。

Interesting Posts