如何在NodeJS中测试递归调用的函数? [英] How to test a recursively called function in NodeJS?

查看:267
本文介绍了如何在NodeJS中测试递归调用的函数?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个用ES6/7编写的循环函数,该函数由babel编译.我创建了一个循环功能,使用猫鼬检查用户文档是否存在.

I have a recurring function written in ES6/7 which is transpiled by babel. I have a recurring function created that checks if there is a user document, using mongoose.

// Keep checking if there is a user, if there is let execution continue
export async function checkIfUserExists(){
  let user = await User.findOneAsync({});
  // if there is no user delay one minute and check again
  if(user === null){
    await delay(1000 * 60 * 1)
    return checkIfUserExists()
  } else {
    // otherwise, if there a user, let the execution move on
    return true
  }
}

如果没有用户,我正在使用delay库将执行延迟一分钟,然后递归调用该函数.

If there is no user I am using the delay library to delay execution for a minute upon which the function is called recursively.

这可以暂停执行整个功能,直到找到用户为止:

This allows the halting of execution of the overall function until a user is found:

async function overallFunction(){
  await checkIfUserExists()
  // more logic
}

else分支非常易于生成测试.如何为if分支创建一个测试以验证递归是否正常工作?

The else branch is very easy to generate tests for. How do I create a test for the if branch that verifies the recursion is working properly?

目前,在测试过程中,我已用proxyquire替换了delay方法,将其替换为仅返回值的自定义延迟函数.到那时,我可以将代码更改如下:

At the moment I have replaced the delay method with proxyquire during testing, subbing it for a custom delay function that just returns a value. At that point I can change the code to look like this:

// Keep checking if there is a user, if there is let execution continue
export async function checkIfUserExists(){
  let user = await User.findOneAsync({});
  // if there is no user delay one minute and check again
  if(user === null){
    let testing = await delay(1000 * 60 * 1)
    if (testing) return false
    return checkIfUserExists()
  } else {
    // otherwise, if there a user, let the execution move on
    return 
  }
}

问题在于正在更改源代码以适应测试.是否有更好,更清洁的解决方案?

Issue there is that the source code is being changed to accommodate for the test. Is there a better, cleaner solution?

推荐答案

我写了一个示例,说明如何在此处测试递归调用的函数:

I've written an example of how you could test your recursively called function here:

https://jsfiddle.net/Fresh/qppprz20/

该测试使用了 Sinon javascript测试库.您可以在第n次调用中设置存根的行为,因此可以模拟何时不返回用户,然后模拟何时返回用户,例如

This test makes use of the Sinon javascript test library. You can set-up the behaviour of the stub on the nth call, hence you can simulate when no user is returned and then subsequently when a user is returned e.g.

// Stub the method behaviour using Sinon javascript framework
var user = new User();
var userStub = sinon.stub(user, 'findOneAsync');
userStub.onFirstCall().returns(null);
userStub.onSecondCall().returns({});

因此,onFirstCall模拟第一个调用,onSecondCall模拟递归调用.

Hence onFirstCall simulates the first call and onSecondCall the recursive call.

请注意,在完整示例中,我简化了checkIfUserExists,但是相同的测试前提将适用于您的完整方法.另外请注意,您还必须使用延迟方法.

Note that in the full example I've simplified checkIfUserExists, but the same test premise will apply for your full method. Also note that you would additionally have to stub your delay method.

这篇关于如何在NodeJS中测试递归调用的函数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆