lundi 11 juillet 2016

Sinon.js: How to mock a object constructed with new?

I have this code:

var async = require('async'),
    util = require('util');

var Parse = require('parse/node');

function signup(userInfo, callback) {
    var username = userInfo.username,
        email = userInfo.email,
        password = userInfo.password;

    var user = new Parse.User();
    user.set('username', username);
    user.set('email', email);
    user.set('password', password);

    user.signUp(null, {
        success: (user) => {
            // console.log('BaaS Signup success ' + util.inspect(user));
            callback(null, user);
        },
        error: (user, error) => {
            // console.log('BaaS Signup error ' + util.inspect(error));
            callback(JSON.stringify(error));
        }
    });
}

I want to unit test it, and as such need to be able to test the content of the success and error function.

If user was a library, I could use proxyquire and put a stub returning a promise and be done with it, however, user is constructed.

How can I stub Parse.User() so that I can control the resulting object?

Aucun commentaire:

Enregistrer un commentaire