簡體   English   中英

用 sinon 測試內部 function

[英]Testing an inside function with sinon

我需要測試一個名為“緩存”的 function。 It is a function wrapper, which takes a function and caches its results depending on the arguments, that were applied to the function.

這就是我實現 function 的方式

const cache = (func) => {
    let cache = {};
    let stringFromArgs;
    return (...args) => {
        let n = args[0];
        if (stringFromArgs === JSON.stringify(args)) {
           return cache[n];
        } else  {
            stringFromArgs = JSON.stringify(args)
            cache[n] = func(...args);
            return cache[n];
        }
    }
}

這就是它的工作原理:

let complexFunction = function(arg1, arg2) {
    return arg1 + arg2;
 };



let cachedFunction = cache(complexFunction);


cachedFunction(1, 2); // complex function should be executed
cachedFunction(1, 2); // complex function should not be invoked again,
                      // instead the cached result should be returned
cachedFunction(1, 5); // should be executed, because the method wasn't invoked before with these arguments

當 function cachedFunction 使用相同的 arguments 調用兩次時,function 內部的那個應該只調用一次。 第二次結果應該來自緩存。 我需要使用 sinon.js 進行測試。 或者也許有另一種方法來測試它?

這是單元測試解決方案:

cache.js

const cache = (func) => {
  let cache = {};
  let stringFromArgs;
  return (...args) => {
    let n = args[0];
    if (stringFromArgs === JSON.stringify(args)) {
      return cache[n];
    } else {
      stringFromArgs = JSON.stringify(args);
      cache[n] = func(...args);
      return cache[n];
    }
  };
};

module.exports = cache;

cache.test.js

const cache = require('./cache');
const sinon = require('sinon');
const { expect } = require('chai');

describe('62426207', () => {
  it('should call complex function', () => {
    let complexFunction = sinon.stub().callsFake((arg1, arg2) => {
      return arg1 + arg2;
    });
    let cachedFunction = cache(complexFunction);
    cachedFunction(1, 2);
    sinon.assert.calledWith(complexFunction, 1, 2);
    sinon.assert.calledOnce(complexFunction);
  });

  it('should not call complex function again', () => {
    let complexFunction = sinon.stub().callsFake((arg1, arg2) => {
      return arg1 + arg2;
    });
    let cachedFunction = cache(complexFunction);
    cachedFunction(1, 2);
    let ret = cachedFunction(1, 2);
    expect(ret).to.be.equal(3);
    sinon.assert.calledWith(complexFunction, 1, 2);
    sinon.assert.calledOnce(complexFunction);
  });

  it('should call complex function if arguments are different', () => {
    let complexFunction = sinon.stub().callsFake((arg1, arg2) => {
      return arg1 + arg2;
    });
    let cachedFunction = cache(complexFunction);
    let ret1 = cachedFunction(1, 2);
    expect(ret1).to.be.equal(3);
    let ret2 = cachedFunction(1, 5);
    expect(ret2).to.be.equal(6);
    sinon.assert.calledWith(complexFunction, 1, 2);
    sinon.assert.calledWith(complexFunction, 1, 5);
    sinon.assert.calledTwice(complexFunction);
  });
});

覆蓋率 100% 的單元測試結果:

  62426207
    ✓ should call complex function
    ✓ should not call complex function again
    ✓ should call complex function if arguments are different


  3 passing (18ms)

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM