0

Consider a class which I want to test

class A{
 private b:B = new B();
 
 function someFunction(){
  b.someOtherFunction(); // prevent this call
  return 42;
 }
}
Class B{
  function someOtherFunction(){
  //does stuff
 }
}

Now I want to test someFunction of class A, how do I prevent someOtherFunction of class B, being called. I do not have access to object b, since it is private.

const a = new A();
describe("A",()=>{
 it("test someFunction", ()=>{
   sinon.stub(B,"someOtherFunction").resolves()
   // test someFunction here
 })
})

wont work because sinon.stub expects the object b, not the class B.

  • Does this answer your question https://stackoverflow.com/questions/21072016/stubbing-a-class-method-with-sinon-js? – Lin Du Jan 19 '23 at 03:17

1 Answers1

0

So here's what I did

class A{
 private b:B = new B();
 
 function someFunction(){
  callSomeOtherFunction(b);// prevent this call
  return 42;
 }

 function callSomeOtherFunction(objectB : B){
  objectB.someOtherFunction();
 }

Now I can easily stub it like

sinon.stub(a,"callSomeOtherFunction");