如何存根节点缓存的cache.get()?

时间:2019-10-07 23:08:42

标签: node.js testing mocha sinon chai

我正在为使用node-cache的函数编写单元测试。 在以下功能中,

  1. 我想在第一个cache.get处返回一个字符串
  2. 第二个cache.get中的数组

请注意,我从testFunction中删除了部分代码,因为它与我的问题无关。

const NodeCache          = require('node-cache');
const cache              = new NodeCache();
...
const testFunction = () => {
  let myStringCache = cache.get('cacheName1');
  let myArrayCache = cache.get('cacheName2');

   ... Do something with caches ...

   return 'return something';
}

...
module.exports = {
   ...,
   testFunction,
   ...
}

我创建了以下测试

describe('sample test with testFunction() ', ()=>{
  let stubCache;
  let stub;
  before((done)=>{
    sandbox = sinon.createSandbox();
    stubCache = sandbox.stub(cache, 'get');
    stubCache.withArgs('cacheName1').returns('sample string');
    stubCache.withArgs('cacheName2').returns([1,2,3,4,5,6]);
    stub = proxyquire('./filelocation.js',{
      'node-cache': stubCache
    });
    done();
  });

  it('should not throw error',(done)=>{
    chai.expect(stub.testFunction()).not.to.throw;
  });
})

我正在使用Google搜索,有一些解决方案可以使用proxyquire对值进行存根。但看起来确实存在存根,但不是我想要的位置。 它在NodeCache存根,但在cache

所以我有问题:

  1. 有人知道如何将cache.get()mochachaisinon存根吗?如果是这样,请分享您的做法?
  2. 是否可以通过cache.get()的参数对不同的收益进行存折?

1 个答案:

答案 0 :(得分:1)

这是单元测试解决方案:

index.js

const NodeCache = require("node-cache");
const cache = new NodeCache();

const testFunction = () => {
  let myStringCache = cache.get("cacheName1");
  let myArrayCache = cache.get("cacheName2");
  console.log("myStringCache:", myStringCache);
  console.log("myArrayCache:", myArrayCache);
  return "return something";
};

module.exports = {
  testFunction
};

index.spec.js

const sinon = require("sinon");
const proxyquire = require("proxyquire");
const chai = require("chai");

describe("sample test with testFunction() ", () => {
  let stubCache;
  let stub;
  let getCacheStub;
  before(() => {
    sandbox = sinon.createSandbox();
    getCacheStub = sandbox.stub();
    stubCache = sandbox.stub().callsFake(() => {
      return {
        get: getCacheStub
      };
    });
    getCacheStub.withArgs("cacheName1").returns("sample string");
    getCacheStub.withArgs("cacheName2").returns([1, 2, 3, 4, 5, 6]);
    stub = proxyquire("./", {
      "node-cache": stubCache
    });
  });

  it("should not throw error", () => {
    const logSpy = sinon.spy(console, "log");
    chai.expect(stub.testFunction()).not.to.throw;
    sinon.assert.calledWith(
      logSpy.firstCall,
      "myStringCache:",
      "sample string"
    );
    sinon.assert.calledWith(logSpy.secondCall, "myArrayCache:", [
      1,
      2,
      3,
      4,
      5,
      6
    ]);
  });
});

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

  sample test with testFunction() 
myStringCache: sample string
myArrayCache: [ 1, 2, 3, 4, 5, 6 ]
    ✓ should not throw error


  1 passing (87ms)

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

源代码:https://github.com/mrdulin/mocha-chai-sinon-codelab/tree/master/src/stackoverflow/58278211