Sinon Spy is not called if the spied method is called indirectly

后端 未结 1 945
花落未央
花落未央 2021-01-11 15:15

Problem

In our codebase we have a problem with sinon which can be reproduced with the code snipped below. The thing is that it seems to be that indirect called spi

1条回答
  •  礼貌的吻别
    2021-01-11 15:48

    The problem is that myModule has two private functions called method() and methodCaller() as well as two methods it exposes with the same names.

    Sinon is capable on spying on the exposed methods, but not the internal functions.

    When you call module.method() your invoking the exposed method, so Sinon is able to detect the call. However, when you call method.methodCaller(), methodCaller() calls the method() private function directly, and therefore the call is not detected by Sinon.

    If you change your methodCaller() function to:

    methodCaller = function(){
        this.method();
    }
    

    ... then Sinon should be able to capture the "indirect" call to method() when calling methodCaller().

    0 讨论(0)
提交回复
热议问题