为nodejs中的azure redis缓存编写单元测试

roejwanj  于 2021-06-09  发布在  Redis
关注(0)|答案(1)|浏览(424)

我正在尝试使用mocha框架编写一个单元测试用例,在这里我必须模拟azureredis缓存…有人能帮助我如何使用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();
kupeojn6

kupeojn61#

以下是单元测试解决方案: 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 |                   
----------|---------|----------|---------|---------|-------------------

相关问题