Stubbing a class method with Sinon.js

后端 未结 4 1991
无人共我
无人共我 2020-11-30 18:58

I am trying to stub a method using sinon.js but I get the following error:

Uncaught TypeError: Attempted to wrap undefined property sample_pressure as function

4条回答
  •  温柔的废话
    2020-11-30 19:31

    The top answer is deprecated. You should now use:

    sinon.stub(YourClass.prototype, 'myMethod').callsFake(() => {
        return {}
    })
    

    Or for static methods:

    sinon.stub(YourClass, 'myStaticMethod').callsFake(() => {
        return {}
    })
    

    Or for simple cases just use returns:

    sinon.stub(YourClass.prototype, 'myMethod').returns({})
    
    sinon.stub(YourClass, 'myStaticMethod').returns({})
    

    Or if you want to stub a method for an instance:

    const yourClassInstance = new YourClass();
    sinon.stub(yourClassInstance, 'myMethod').returns({})
    

提交回复
热议问题