[removed] Mocking Constructor using Sinon

前端 未结 8 2253
迷失自我
迷失自我 2021-02-06 20:47

I am pulling my hair out trying to figure out how to mock a constructor using sinon. I have a function that will create multiple widgets by calling a constructor that accepts a

8条回答
  •  清歌不尽
    2021-02-06 21:18

    Using Sinon 4.4.2, I was able to mock an instance method like this:

    const testObj = { /* any object */ }
    sinon.stub(MyClass.prototype, "myMethod").resolves(testObj)
    let myVar = await new MyClass(token).myMethod(arg1, arg2)
    // myVar === testObj
    

    A similar solution provided here: Stubbing a class method with Sinon.js

提交回复
热议问题