简体   繁体   中英

sinon stubbed class not found

I started to write unit test cases using sinon and facing the following problem.

myfile.js

module.exports = class A{
    constructor(classB_Obj){
        this.classBobj = classB_Obj;
        classBobj.someFunctionOfClassB(); // error coming here
    }
    doSomething(){

    }
}

where class B is in

myfile2.js

module.exports = class B{
    constructor(arg1, arg2){
        this.arg1 = arg1;
        this.arg2 = arg2;
    }
    someFunctionOfClassB(){

    }
}

when I test class A and use sinon to stub Class B

const myfile2 = require('../myfile2').prototype;
const loggerStub = sinon.stub(myfile2, 'someFunctionOfClassB');

while executing it gives exception

classBobj.someFunctionOfClassB is not a function.

What is the correct way of stubbing it? I don't want to instantiate class B.

This has really nothing to do with stubbing.

You have to define this function as a static method to achieve this:

module.exports = class B{
    constructor(arg1, arg2){
        this.arg1 = arg1;
        this.arg2 = arg2;
    }
    static someFunctionOfClassB(){

    }
}

Then you can call the method on the class object.

When you write a normal class method you always have to instantiate the class before you can you use it on the instance:

const b = new class_Obj();
b.someFunctionOfClassB();

See also: Class vs. static method in JavaScript

Here is the unit test solution:

myfile.js :

module.exports = class A {
  constructor(classB_Obj) {
    this.classBobj = classB_Obj;
    this.classBobj.someFunctionOfClassB();
  }
  doSomething() {}
};

myfile2.js :

module.exports = class B {
  constructor(arg1, arg2) {
    this.arg1 = arg1;
    this.arg2 = arg2;
  }
  someFunctionOfClassB() {}
};

myfile.test.js :

const A = require("./myfile");
const B = require("./myfile2");
const sinon = require("sinon");

describe("52559903", () => {
  afterEach(() => {
    sinon.restore();
  });
  it("should pass", () => {
    const bStub = sinon.createStubInstance(B, {
      someFunctionOfClassB: sinon.stub(),
    });
    new A(bStub);
    sinon.assert.calledOnce(bStub.someFunctionOfClassB);
  });
});

Unit test result with coverage report:

 myfile
    ✓ should pass


  1 passing (10ms)

----------------|----------|----------|----------|----------|-------------------|
File            |  % Stmts | % Branch |  % Funcs |  % Lines | Uncovered Line #s |
----------------|----------|----------|----------|----------|-------------------|
All files       |     87.5 |      100 |    57.14 |     87.5 |                   |
 myfile.js      |      100 |      100 |       50 |      100 |                   |
 myfile.test.js |      100 |      100 |      100 |      100 |                   |
 myfile2.js     |    33.33 |      100 |        0 |    33.33 |               3,4 |
----------------|----------|----------|----------|----------|-------------------|

Source code: https://github.com/mrdulin/mocha-chai-sinon-codelab/tree/master/src/stackoverflow/52559903

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM