单元测试函数并模拟事件发射器响应

时间:2015-01-22 21:20:20

标签: javascript node.js unit-testing

我目前正在使用mocha编写一些针对node.js中显示的代码的测试。我想模拟事件发射器的响应' check_user'。我有sinon.js,但我无法在正确的地方找到最好的方法来模拟响应。

任何人都可以就如何解决此问题提供一些建议吗?

TgCustomCommand.contact = new Command("contact", "Will send you a users contact card to add to your contact list. Usage is .contact nick", function (input, callback) {
var payload = input;
//Switch our contact to payload.to as this is what our checkuser function looks at

//Check the command over
if (payload.command_args === false) {
    payload.response = 'msg ' + payload.to + ' ' + this.description;
    return callback(null, payload);
} else {

    payload.return = payload.to;
    payload.to = payload.command_args; //Set up ready for nick check
    //Check the nick exists
    emitter.emit('check_user', payload, function (err, result) {
        if (err) return callback(err, null);
        payload = input; //Reset our payload so we have correct payload.to


        //Check how many users we returned
        if (result.length === 0) { //Not in our contact list

            payload.response = 'msg ' + payload.return + ' I do not have that person in my contact list!';
            return callback(null, payload);

        } else if (result.length === 1) {
            payload.to = result[0].Nick;

            payload.response = "send_contact " + payload.return + ' ' + result[0].Phone + ' ' + result[0].Nick + " _";

            return callback(null, payload);
        }

        else {
            //loop through our object and create a list of those returned
            payload.response = "msg " + payload.return + " I know multiple people with a similar nick: ";
            for (var i = 0; i < result.length; i++) {
                log.debug(result[i].Nick);
                payload.response = payload.response + result[i].Nick + "  ";
            }

            return callback(null, payload);
        }


    });

}
;

});

0 个答案:

没有答案