使用摩卡进行异步瀑布式单元测试 [英] unit testing async-waterfall using mocha

查看:70
本文介绍了使用摩卡进行异步瀑布式单元测试的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用Mocha作为我的node.js应用程序的测试框架.

I am using Mocha as a testing framework for my node.js application.

我有一个使用异步瀑布节点模块的现有模块,我正在尝试为此编写一个单元测试用例.我无法为数组中的第二个功能编写测试用例.关于如何将一个函数的结果传递给数组中下一个函数的任何提示 var async = require('async');

I have an existing module that uses async-waterfall node module.I am trying to write a unit test case for it. I am unable to write a test case for the second function in the array. Any tips on how would I pass the result of one function to the next function in the array var async = require('async');

module.exports.resetPassword = function(req,res,next){
 async.waterfall([
  function(done){
    var token = do something;
    done(err,token);
  },
   function(token,done){
       //do some stuff
     res.status(200).send('success');
   },
    ]function(err){ 
     //send failure response
     });
};

推荐答案

我想我现在已经理解了这个问题. 在测试中-执行以下行:

I think I understand the issue now. In your test - you execute the line:

user.resetPassword(request,response,next);

紧随其后的那一行:

cryptoStub.restore();

问题是-async.waterfall将启动第一个方法,但是在运行第二个方法之前,它将运行单元测试的下一行,因此您看不到下一个方法.解决该问题的代码如下:

The issue is - that async.waterfall will start the first method, but before it runs the second one, it will run the next line from the unit test, and therefore you don`t see the next method. Code that will solve it will be like:

user.resetPassword(request,response,function(err) {
    cryptoStub.restore();
    done();
});

看起来更好吗?

这篇关于使用摩卡进行异步瀑布式单元测试的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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