sinon.js:存根调用不会增加callCount

问题描述

sinon.js 9.0.2

在测试中,我创建一个存根:

test.js

const myHandler = {
  handle: (some_var_to_pass) => sinon.stub()["resolves"]([{ whatever: "whatever" }]),};

然后作为某些终结点请求(使用async / await)从测试中获得实际代码,该代码将执行以下代码

code.js

module.exports = (myHandler) => ({
  getStuff: async (req,res,next) => {
  const var1 = "something1";
  const var2 = "something2";
  const my_response = await myHandler.handle()(var1,var2);

  console.log("my response",JSON.stringify(my_response)); //correct,returns { whatever: "whatever"}
  console.log("Executed stub: ",myHandler.handle(some_var_to_pass).callCount); //always returns zero

我控制myHandler.handle正确地完成了存根的创建,并且my_response

{ whatever: "whatever"}

但是,myHandler.handle().callCount保持为零(不是undefined)。我缺少什么使它正确递增?还是在创建存根时将.callCount实际设置为1(会很奇怪)?

解决方法

您没有将myHandler.handle方法存根。您只需对myHandler.handle方法返回的函数进行存根处理。单元测试解决方案:

code.js

module.exports = (myHandler) => ({
  getStuff: async (req,res,next) => {
    const var1 = 'something1';
    const var2 = 'something2';
    const my_response = await myHandler.handle()(var1,var2);
    return my_response;
  },});

code.test.js

const code = require('./code');
const sinon = require('sinon');

describe('64468517',() => {
  it('should pass',async () => {
    const returnedFn = sinon.stub().resolves([{ whatever: 'whatever' }]);
    const myHandler = {
      handle: sinon.stub().returns(returnedFn),};
    await code(myHandler).getStuff();
    expect(myHandler.handle.callCount).to.be.eq(1);
    sinon.assert.calledOnce(myHandler.handle);
    sinon.assert.calledWithExactly(returnedFn,'something1','something2');
  });
});

单元测试结果:

  64468517
    ✓ should pass


  1 passing (13ms)

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

因为当您有以下行:myHandler.handle(some_var_to_pass)时,您将启动新的存根,该存根当然具有0个callCount。

解决方法:将myHandler.handle(some_var_to_pass)保存到变量,调用并期望使用该变量。

示例:

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

const myHandler = {
  // Call myHandler.handle() will return sinon.stub which resolves to array object.
  handle: () => sinon.stub()['resolves']([{ whatever: 'whatever' }]),};

describe('myHandler',function() {
  it('handle',async function() {
    const var1 = 'something1';
    const var2 = 'something2';
    // Get the stub variable.
    const stub = myHandler.handle();
    // const result = await myHandler.handle()(var1,var2);
    // You use stub variable.
    const result = await stub(var1,var2);
    // Make sure the result is correct.
    expect(result).to.deep.equal([{ whatever: 'whatever' }]);
    // Obviously,stub callCount should be 1.
    expect(stub.callCount).to.equal(1);
  });
});

当我在终端上使用mocha运行它时:

$ npx mocha index.test.js 


  myHandler
    ✓ handle


  1 passing (3ms)