简体   繁体   English

使用Mocha进行单元测试异步瀑布

[英]unit testing async-waterfall using mocha

I am using Mocha as a testing framework for my node.js application. 我正在使用Mocha作为我的node.js应用程序的测试框架。

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'); 关于如何将一个函数的结果传递给数组中下一个函数的任何提示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);

And immediately after that - the line: 然后紧接着-行:

cryptoStub.restore();

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. 问题是-async.waterfall将启动第一个方法,但是在运行第二个方法之前,它将运行单元测试的下一行,因此您看不到下一个方法。 Code that will solve it will be like: 将解决它的代码将类似于:

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

Does it look better? 看起来好点吗?

My code is as follows. 我的代码如下。 My issue is around writing a unit test case for this method. 我的问题是为这种方法编写单元测试用例。 I am using mocha along with sinon for stubbing. 我正在将摩卡咖啡与sinon一起使用。 The unit test that I have written is 我写的单元测试是

Unit Test 单元测试

  it("Should return an error if there is an error in retrieving the user details from the db",function(done)
{
    request={
        body:{
            email:'test@test.com'

        }
    };

    var expectedMessageFromCrypto = '12121212121';
    var cryptoStub = sinon.stub(crypto, "randomBytes",function(err,callback){
        callback(null,expectedMessageFromCrypto);
    });

    user.resetPassword(request,response,next);

    cryptoStub.restore();
    done();
});
});

The resetPassword module resetPassword模块

methods.exports.resetPassword = function (req, res, next) {
var token = null;
async.waterfall([
    function (done) {
        crypto.randomBytes(16, function (err, buf) {
        if(err){
            return res.status(400);
        }
        else{
            token = buf.toString('hex');
        }
        done(err, token);
        });
    },
    function (token, done) {            
    var user = getUserInformation(req.body.email);
        sendEmail(user, done);
    }
], function (err) {
    if (err) {
        logger.error('error in sending confirmation mail');
        return next(err);
    }        
    res.status(200).send('success');
});
};

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM