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



我正在尝试使用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();

这是单元测试解决方案:

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 |                   
----------|---------|----------|---------|---------|-------------------

最新更新