Stubbing a class method with Sinon.js

后端 未结 4 1999
无人共我
无人共我 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:42

    I ran into the same error trying to mock a method of a CoffeeScript class using Sinon.

    Given a class like this:

    class MyClass
      myMethod: ->
        # do stuff ...
    

    You can replace its method with a spy this way:

    mySpy = sinon.spy(MyClass.prototype, "myMethod")
    
    # ...
    
    assert.ok(mySpy.called)
    

    Just replace spy with stub or mock as needed.

    Note that you'll need to replace assert.ok with whatever assertion your testing framework has.

提交回复
热议问题