【问题标题】:Writing unit test for the azure redis cache in nodejs在 nodejs 中为 azure redis 缓存编写单元测试
【发布时间】:2020-07-07 06:31:29
【问题描述】:

我正在尝试使用 mocha 框架编写一个单元测试用例,我必须在其中模拟一个 azure redis 缓存...有人可以帮助我如何使用 node.js 模拟用于单元测试用例的缓存。

    const redis = require('redis'); 
    const redisHostName = process.env['hostName'];
    const redisPort = process.env['port'];
    const redisKey = process.env['key'];

    let client = redis.createClient(redisPort, redisHostName, { auth_pass: redisKey, tls: { serverName: redisHostName } });



    async function getKeyValue(key, ctx) {
        context = ctx;
        return new Promise((resolve, reject) => {
            client.get(key, function (err, result) {
            if (err) {
                resolve(err);
            }
                resolve(result);
            }); 
        });
    }
getKeyValue();

【问题讨论】:

  • 那么,你想测试getKeyValue函数吗?

标签: node.js redis sinon stub azure-redis-cache


【解决方案1】:

这里是单元测试解决方案:

index.js

const redis = require('redis');
const redisHostName = process.env['hostName'];
const redisPort = process.env['port'];
const redisKey = process.env['key'];

let client = redis.createClient(redisPort, redisHostName, { auth_pass: redisKey, tls: { serverName: redisHostName } });

async function getKeyValue(key, ctx) {
  context = ctx;
  return new Promise((resolve, reject) => {
    client.get(key, function(err, result) {
      if (err) {
        return resolve(err);
      }
      resolve(result);
    });
  });
}

exports.getKeyValue = getKeyValue;

index.test.js:

const sinon = require('sinon');
const redis = require('redis');

describe('60870408', () => {
  beforeEach(() => {
    process.env['hostName'] = '127.0.0.1';
    process.env['port'] = 6379;
    process.env['key'] = 'test';
  });
  afterEach(() => {
    delete require.cache[require.resolve('./')];
    sinon.restore();
  });
  it('should get value', async () => {
    const client = {
      get: sinon.stub().callsFake((key, callback) => callback(null, 'elsa')),
    };
    const createClientStub = sinon.stub(redis, 'createClient').returns(client);
    const { getKeyValue } = require('./');
    sinon.assert.calledWithExactly(createClientStub, '6379', '127.0.0.1', {
      auth_pass: 'test',
      tls: { serverName: '127.0.0.1' },
    });
    const actual = await getKeyValue('name', {});
    sinon.assert.match(actual, 'elsa');
    sinon.assert.calledWithExactly(client.get, 'name', sinon.match.func);
  });

  it('should handle error', async () => {
    const mError = new Error('network');
    const client = {
      get: sinon.stub().callsFake((key, callback) => callback(mError)),
    };
    const createClientStub = sinon.stub(redis, 'createClient').returns(client);
    const { getKeyValue } = require('./');
    sinon.assert.calledWithExactly(createClientStub, '6379', '127.0.0.1', {
      auth_pass: 'test',
      tls: { serverName: '127.0.0.1' },
    });
    const actual = await getKeyValue('name', {});
    sinon.assert.match(actual, mError);
    sinon.assert.calledWithExactly(client.get, 'name', sinon.match.func);
  });
});

100% 覆盖率的单元测试结果:

  60870408
    ✓ should get value
    ✓ should handle error


  2 passing (28ms)

----------|---------|----------|---------|---------|-------------------
File      | % Stmts | % Branch | % Funcs | % Lines | Uncovered Line #s 
----------|---------|----------|---------|---------|-------------------
All files |     100 |      100 |     100 |     100 |                   
 index.js |     100 |      100 |     100 |     100 |                   
----------|---------|----------|---------|---------|-------------------

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2021-11-15
    • 2021-04-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-06-28
    • 1970-01-01
    相关资源
    最近更新 更多