nodejs中的Azure Redis缓存的编写单元测试

问题描述 投票:0回答:1

我正在尝试使用mocha框架编写单元测试用例,在这里我必须模拟一个蔚蓝的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();
node.js redis sinon stub azure-redis-cache
1个回答
0
投票

这是单元测试解决方案:

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 |                   
----------|---------|----------|---------|---------|-------------------
© www.soinside.com 2019 - 2024. All rights reserved.