使用retry_strategy单元测试的nodejs redisClient

时间:2018-11-13 03:57:34

标签: node.js unit-testing redis

我正在使用Sinon和Mocha和NYC报告进行学习单元测试。我完全混淆了如何覆盖此代码

let client = redis.createClient({
    retry_strategy: function(options) {
        if (options.error) {
            if (options.error.code === 'ECONNREFUSED') {
                // End reconnecting on a specific error
                // and flush all commands with a individual error
                return new Error('The server refused the connection');
            }
            if (options.error.code === 'ECONNRESET') {
                return new Error('The server reset the connection');
            }
            if (options.error.code === 'ETIMEDOUT') {
                return new Error('The server timeouted the connection');
            }
        }
        if (options.total_retry_time > 1000 * 60 * 60) {
            // End reconnecting after a specific timeout and flush all commands
            // with a individual error
            logger.error('Retry time exhausted')
            return new Error('Retry time exhausted');
        }
        if (options.attempt > 10) {
            // End reconnecting with built in error
            logger.error('Retry attempt exceed')
            return undefined;
        }
        // reconnect after
        return Math.min(options.attempt * 100, 3000);
    },
    ...config, // my function pass this arguments
})

我尝试对redis.createClient进行存根处理,但似乎并没有涵盖retry_strategy(作为参数),并且最终也未公开覆盖率报告。

对我有任何想法或想法如何涵盖retry_strategy陈述和分支?或任何其他方法来代替存根?

谢谢

0 个答案:

没有答案