One solution is to rearchitect your code so that it is more testable. You could create an AddPropProvider
that does the actual work (with implementation details more exposed for testing) and pass it in to the constructor for MyCtrl
. That's the classic inversion of control strategy.
Another solution is maybe just make that function public after all? Private is ideal for hiding implementation details that could change, but maybe you know that function is always going to be there anyways.
Finally there's the hack. That function really exists on the instances of MyCtrl
and the compiler only doesn't want you calling it because you said so.
var m = new MyCtrl();
(<any>m).addProp();
Now if you remove or modify the signature for addProp
that code will still compile so you've lost type safety. In most code that would be bad. But this is unit test code so we're going to learn about the break early and easily so it's not horrible.