lundi 24 août 2015

Use Mocha/Chai to test NodeJS anonymous callback code

I'm new to unit testing. I'm working in NodeJS and I'm using the NodeJS "async" library found HERE. Here's the code I'm working with:

module.exports = {

    postYelp: function(cb, results){
if(results.findLocation){
    results.client.post(['resources', 'Yelp'].join('/'), results.findLocation, function(err, data, ctx){
        /* istanbul ignore next  */
        if(err){
            cb(err);
        }
        else{
            console.log('data', data);
            console.log('ctx', ctx.statusCode);
            return cb(null, ctx.statusCode);
        }
    });
}
/* istanbul ignore next  */
else cb(null);
},
}

So as you can see, the the 3rd argument in the function call to results.client.post is an anonymous callback. I want test coverage for this callback. If I could easily refactor to create a named function with the same code as the callback and replace it, I could test it separately. However, the enclosing function ("postYelp") has its own callback ("cb") that must be used inside the anonymous callback function. How can I unit test this anonymous function code?

Aucun commentaire:

Enregistrer un commentaire