繁体   English   中英

存根函数与带有sinon的返回函数?

[英]Stub function with return function with sinon?

我想进行单元测试并覆盖我的代码,这是我的代码,如何用sinon覆盖createClient?

const client = redis.createClient({
  retry_strategy: function(options) {
    if (options.error) {
      if (options.error.code === 'ECONNREFUSED') {
        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) {
      return new Error('Retry time exhausted');
    }
    if (options.attempt > 10) {
      return undefined;
    }
    return Math.min(options.attempt * 100, 3000);
  }

测试分配给retry_strategy的函数的最简单方法是将其移到redis.createClient调用之外并导出它:

export const retryStrategy = function (options) {
  if (options.error) {
    if (options.error.code === 'ECONNREFUSED') {
      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) {
    return new Error('Retry time exhausted');
  }
  if (options.attempt > 10) {
    return undefined;
  }
  return Math.min(options.attempt * 100, 3000);
}

const client = redis.createClient({
  retry_strategy: retryStrategy
  ...

然后,您可以导入它并直接对其进行测试:

import { retryStrategy } from './your-module';

test('retryStrategy', () => {
  expect(retryStrategy({ attempt: 5 })).toBe(500);  // SUCCESS
  ...
})

您可以执行的另一种方法是使用proxyquire模拟redis.createClient以返回opt以便我们访问retry_strategy 在测试中,我们调用retry_strategy并传递其options

// test.js
const proxyquire = require('proxyquire');
const src = proxyquire('./your-source-file', { 'redis': { createClient(opt) { 
  return opt 
}}});
const chai = require('chai');
const expect = chai.expect;

describe('testing redis ', function() {
  it('refuses connection', function() {
    const options = {
      error: {
        code: 'ECONNREFUSED'
      }
    }

    expect(src.retry_strategy(options).message).to.equal('The server refused the connection');            
  });
});

这是我用于测试的源文件

// source.js
const redis = require('redis');

const client = redis.createClient({
  retry_strategy: function(options) {
    if (options.error) {
      if (options.error.code === 'ECONNREFUSED') {
        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) {
      return new Error('Retry time exhausted');
    }
    if (options.attempt > 10) {
      return undefined;
    }
    return Math.min(options.attempt * 100, 3000);
  }
});

module.exports = client;

暂无
暂无

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

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