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

How to stub node-cache's cache.get()?

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

  1. 我想要一个字符串 return 在第一个 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;
  });
})

我在谷歌上搜索了一下,有一些部分解决方案可以使用 proxyquire 来存根该值。但看起来它确实存根但它不是我想要的地方。 它存根于 NodeCachecache

所以我有问题:

  1. 有人知道如何用 mochachaisinon 存根 cache.get() 吗?如果是这样,请分享你是怎么做到的?
  2. 是否可以通过cache.get()的参数对不同的return进行存根?

单元测试解决方案如下:

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/Whosebug/58278211