There is no reason why you cannot access a private function outside the context of your instance.
Btw, it is not a good idea to spy on objects that you want to check. When you check to see if any particular method in your class is called that you want to test, it says nothing. Suppose you wrote a test and it passed, after two weeks you refactor some things in a function and add an error. So your test is still green because you are calling the function. IN
Spies are useful when you work with Dependency Injection , where all external dependencies are passed by the constructor and not created in your class. So let's say that you have a class that needs a dom element. Normal to use this element in jquery selector. But how do you want to verify that something is done with this element? Of course, you can add it to your html test pages. But you can also call your class by passing an element in the constructor. That way, you can use a spy to check if your class interacts with this element as you expected.
source share