How to write unit testing for Angular / TypeScript for private methods with Jasmine

How do you test a private function in angular 2 ?

class FooBar {

    private _status: number;

    constructor( private foo : Bar ) {
        this.initFooBar();

    }

    private initFooBar(){
        this.foo.bar( "data" );
        this._status = this.fooo.foo();
    }

    public get status(){
        return this._status;
    }

}

The solution I found

  1. Put the test code itself inside the closure or Add code inside the closure that stores references to the local variables on existing objects in the outer scope.

    Later strip out the test code using a tool.
    http://philipwalton.com/articles/how-to-unit-test-private-functions-in-javascript/

Please suggest me a better way to solve this problem if you have done any?

P.S

  1. Most of the answer for similar type of question like this one doesn’t give a solution to problem, that’s why I’m asking this question

  2. Most of the developer say you Don’t test private functions but I don’t say they are wrong or right, but there are necessities for my case to test private.

12 Answers
12

Leave a Comment