ChatGPT解决这个技术问题 Extra ChatGPT

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

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

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 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.

I like how half the answers should actually be comments. OP asks question, how do you X? Accepted answer actually tells you how to do X. Then most of the rest turn around and say, not only will I not tell you X (which is clearly possible) but you should be doing Y. Most unit testing tools (I'm not talking about just JavaScript here) are capable of testing private functions/methods. I'll go on to explain why because it seems to have gotten lost in JS land (apparently, given half the answers).
It's good programming practice to break a problem down into manageable tasks, so function "foo(x:type)" will call private functions a(x:type), b(x:type), c(y:another_type) and d(z:yet_another_type). Now because foo, is managing the calls and putting stuff together, it creates a sort of turbulence, like the back sides of rocks in a stream, shadows which are really hard to ensure all the ranges are tested. As such it is easier to ensure that each sub set of ranges is valid, if you try to test the parent "foo" alone the range testing becomes very complicated in cases.
This isn't to say you don't test the public interface, obviously you do, but testing the private methods allow you to test a series of short manageable chunks (the same reason you wrote them in the first place, why would you undo this when it comes to testing), and just because the tests on public interfaces are valid (maybe the calling function restricts the input ranges) doesn't mean that the private methods are not flawed when you add more advanced logic and call them from other new parent functions,
if you tested them properly with TDD you wont be trying to figure out what the hell you were doing later, when you should have tested them correctly.
@Quaternion This comment on TDD dances around OP's question without actually providing any worthwhile insight to the general idea of testing private methods. Sometimes you really do need to access private members to have good test coverage, which is why the Spring Boot test package has reflection utils. Even though public methods are accessing these private members, it's very possible that you can't cover edge cases unless you can directly call the private methods in your unit tests.

A
Aaron Beall

I'm with you, even though it's a good goal to "only unit test the public API" there are times when it doesn't seem that simple and you feel you are choosing between compromising either the API or the unit-tests. You know this already, since that's exactly what you're asking to do, so I won't get into it. :)

In TypeScript I've discovered a few ways you can access private members for the sake of unit-testing. Consider this class:

class MyThing {

    private _name:string;
    private _count:number;

    constructor() {
        this.init("Test", 123);
    }

    private init(name:string, count:number){
        this._name = name;
        this._count = count;
    }

    public get name(){ return this._name; }

    public get count(){ return this._count; }

}

Even though TS restricts access to class members using private, protected, public, the compiled JS has no private members, since this isn't a thing in JS. It's purely used for the TS compiler. Therefor:

You can assert to any and escape the compiler from warning you about access restrictions: (thing as any)._name = "Unit Test"; (thing as any)._count = 123; (thing as any).init("Unit Test", 123); The problem with this approach is that the compiler simply has no idea what you are doing right of the any, so you don't get desired type errors: (thing as any)._name = 123; // wrong, but no error (thing as any)._count = "Unit Test"; // wrong, but no error (thing as any).init(0, "123"); // wrong, but no error This will obviously make refactoring more difficult. You can use array access ([]) to get at the private members: thing["_name"] = "Unit Test"; thing["_count"] = 123; thing["init"]("Unit Test", 123); While it looks funky, TSC will actually validate the types as if you accessed them directly: thing["_name"] = 123; // type error thing["_count"] = "Unit Test"; // type error thing["init"](0, "123"); // argument error To be honest I don't know why this works. This is apparently an intentional "escape hatch" to give you access to private members without losing type safety. This is exactly what I think you want for your unit-testing.

Here is a working example in the TypeScript Playground.

Edit for TypeScript 2.6

Another option that some like is to use // @ts-ignore (added in TS 2.6) which simply suppresses all errors on the following line:

// @ts-ignore
thing._name = "Unit Test";

The problem with this is, well, it suppresses all errors on the following line:

// @ts-ignore
thing._name(123).this.should.NOT.beAllowed("but it is") = window / {};

I personally consider @ts-ignore a code-smell, and as the docs say:

we recommend you use this comments very sparingly. [emphasis original]


So nice to hear a realistic stance on unit testing along with an actual solution rather than you standard unit tester dogma.
Some "official" explanation of the behavior (which even cites unit testing as a use case): github.com/microsoft/TypeScript/issues/19335
Just use` // @ts-ignore` as pointed out below. to tell the linter to ignore the private accessor
@Tommaso Yeah, that's another option, but has the same disadvantage of using as any: you lose all type-checking.
Best answer I've seen in a while, thanks @AaronBeall. And also, thanks tymspy for asking the original question.
M
Mir-Ismaili

You CAN call private methods!

If you encountered the following error:

expect(new FooBar(/*...*/).initFooBar()).toEqual(/*...*/)
// TS2341: Property 'initFooBar' is private and only accessible within class 'FooBar'

just use // @ts-ignore:

// @ts-ignore
expect(new FooBar(/*...*/).initFooBar()).toEqual(/*...*/)

Thank @Moff452 for his/her comment. You can also write:

expect(new FooBar(/*...*/)['initFooBar']()).toEqual(/*...*/)

UPDATE:

@ts-expect-error is a better alternative for @ts-ignore. See: https://www.typescriptlang.org/docs/handbook/release-notes/typescript-3-9.html#ts-ignore-or-ts-expect-error


this should be at the top!
This is certainly another option. It suffers from the same issue as as any in that you lose any type-checking, actually you lose any type-checking on the entire line.
To me the accepted answer is better because the only change you have to write is moving from component.callPrivateMetod() to component['callPrivateMethod]()
I don't like the idea of adding anything to a service just for testing. This is exactly what I was looking for! Who cares about TS for a single line in a test! Typescript suffers more from Typescript than any other reason.
t
tymspy

As most of the developers don't recommend testing private function, Why not test it?.

Eg.

YourClass.ts

export class FooBar {
  private _status: number;

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

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

TestYourClass.spec.ts

describe("Testing foo bar for status being set", function() {

...

//Variable with type any
let fooBar;

fooBar = new FooBar();

...
//Method 1
//Now this will be visible
fooBar.initFooBar();

//Method 2
//This doesn't require variable with any type
fooBar['initFooBar'](); 
...
}

Thanks to @Aaron, @Thierry Templier.


I think typescript gives linting errors when you try to call a private/protected method.
@Gudgip it would give type errors and won't compile. :)
I need help regarding parameterize interface Part { partNumber: string; } test looks like this unpickPart(index: number, part: Part): void { //do stuff } need to call unpick part some how.
b
brainmonger

This worked for me:

Instead of:

sut.myPrivateMethod();

This:

sut['myPrivateMethod']();

Yeah indeed this is the simpler way, you do not need to add any line of code!
How do you pass parameters to method in this case?
M
Martin

Do not write tests for private methods. This defeats the point of unit tests.

You should be testing the public API of your class

You should NOT be testing the implimentation details of your class

Example

class SomeClass {

  public addNumber(a: number, b: number) {
      return a + b;
  }
}

The test for this method should not need to change if later the implementation changes but the behaviour of the public API remains the same.

class SomeClass {

  public addNumber(a: number, b: number) {
      return this.add(a, b);
  }

  private add(a: number, b: number) {
       return a + b;
  }
}

Don't make methods and properties public just in order to test them. This usually means that either:

You are trying to test implementation rather than API (public interface). You should move the logic in question into its own class to make testing easier.


Maybe read the post before commenting on it. I clearly state and demonstrate that testing privates is a smell of testing implementation rather than behavior, which leads to fragile tests.
@user3725805 this is an example of testing the implementation, not the behavior. It would be better to isolate where the private number comes from: a constant, a config, constructor -- and test from there. If the private is does not come from some other source, then it falls into the "magic number" antipattern.
And why is it not allowed to test the implementation? Unit tests are good to detect unexpected changes. When for some reason the constructor forgets to set the number, the test fails immediately and warns me. When someone changes the implementation the test fails too, but I prefer to adopt one test than to have an undetected error.
+1. Great answer. @TimJames Telling the correct practice or pointing out the flawed approach is the very purpose of SO. Instead of finding a hacky-fragile way to achieve whatever the OP wants.
If it's unacceptable to make a private method public just so it can be tested, why is it OK to move the method to its own class!? Both situations involve the testing needs driving implementation structure, except the latter is even more invasive: You have a whole class whose only reason for existence is to let you test something.
D
Deepu Reghunath

call private method using square brackets

Ts file

class Calculate{
  private total;
  private add(a: number) {
      return a + total;
  }
}

spect.ts file

it('should return 5 if input 3 and 2', () => {
    component['total'] = 2;
    let result = component['add'](3);
    expect(result).toEqual(5);
});

1. I have tried your solutions while using squre brackets in nodejs need to use import or require. 2. and my Calculate class was looks like export let cal = new Calculate(); is it possible to access?
This is a very good solution to be able to still keep inteli sense and test the functions
Thanks, the example was short, concise and clear to understand :) It helped me test a private method
C
CTS_AE

As many have already stated, as much as you want to test the private methods you shouldn't hack your code or transpiler to make it work for you. Modern day TypeScript will deny most all of the hacks that people have provided so far.

Solution

TLDR; if a method should be tested then you should be decoupling the code into a class that you can expose the method to be public to be tested.

The reason you have the method private is because the functionality doesn't necessarily belong to be exposed by that class, and therefore if the functionality doesn't belong there it should be decoupled into its own class.

Example

I ran across this article that does a great job of explaining how you should tackle testing private methods. It even covers some of the methods here and how why they're bad implementations.

https://patrickdesjardins.com/blog/how-to-unit-test-private-method-in-typescript-part-2

Note: This code is lifted from the blog linked above (I'm duplicating in case the content behind the link changes)

Before

class User {
    public getUserInformationToDisplay() {
        //...
        this.getUserAddress();
        //...
    }
 
    private getUserAddress() {
        //...
        this.formatStreet();
        //...
    }

    private formatStreet() {
        //...
    }
}

After

class User {
    private address: Address;

    public getUserInformationToDisplay() {
        //...
        address.format();
        //...
    }
}

class Address {
    private format: StreetFormatter;

    public format() {
        //...
        format.toString();
        //...
    }
}

class StreetFormatter {
    public toString() {
        // ...
    }
}

A Note to Close On

You can implicitly test your private methods by making sure that conditions are met such that the code is called through the public interface. If the public interface does not call out to the private methods then that code is not providing any function and should be removed. In the example above there should be some effect that calling the private method should return ie: an object with an address. If there's not, for example the code emits an event in a private method, then you should start looking to decouple that so that it can be tested -- even in that example you would likely listen/subscribe to that event and would be able to test it that way. Decoupling leads to better testability and easier code maintenance later down the road as well.


L
Leon Adler

The point of "don't test private methods" really is Test the class like someone who uses it.

If you have a public API with 5 methods, any consumer of your class can use these, and therefore you should test them. A consumer should not access the private methods/properties of your class, meaning you can change private members when the public exposed functionality stays the same.

If you rely on internal extensible functionality, use protected instead of private.
Note that protected is still a public API (!), just used differently.

class OverlyComplicatedCalculator {
    public add(...numbers: number[]): number {
        return this.calculate((a, b) => a + b, numbers);
    }
    // can't be used or tested via ".calculate()", but it is still part of your public API!
    protected calculate(operation, operands) {
        let result = operands[0];
        for (let i = 1; i < operands.length; operands++) {
            result = operation(result, operands[i]);
        }
        return result;
    }
}

Unit test protected properties in the same way a consumer would use them, via subclassing:

it('should be extensible via calculate()', () => {
    class TestCalculator extends OverlyComplicatedCalculator {
        public testWithArrays(array: any[]): any[] {
            const concat = (a, b) => [].concat(a, b);
            // tests the protected method
            return this.calculate(concat, array);
        }
    }
    let testCalc = new TestCalculator();
    let result = testCalc.testWithArrays([1, 'two', 3]);
    expect(result).toEqual([1, 'two', 3]);
});

R
Ryan Hansen

Sorry for the necro on this post, but I feel compelled to weigh in on a couple of things that do not seem to have been touched on.

First a foremost - when we find ourselves needing access to private members on a class during unit testing, it is generally a big, fat red flag that we've goofed in our strategic or tactical approach and have inadvertently violated the single responsibility principal by pushing behavior where it does not belong. Feeling the need to access methods that are really nothing more than an isolated subroutine of a construction procedure is one of the most common occurrences of this; however, it's kind of like your boss expecting you to show up for work ready-to-go and also having some perverse need to know what morning routine you went through to get you into that state...

The other most common instance of this happening is when you find yourself trying to test the proverbial "god class." It is a special kind of problem in and of itself, but suffers from the same basic issue with needing to know intimate details of a procedure - but that's getting off topic.

In this specific example, we've effectively assigned the responsibility of fully initializing the Bar object to the FooBar class's constructor. In object oriented programming, one of the core tenents is that the constructor is "sacred" and should be guarded against invalid data that would invalidate its' own internal state and leave it primed to fail somewhere else downstream (in what could be a very deep pipeline.)

We've failed to do that here by allowing the FooBar object to accept a Bar that is not ready at the time that the FooBar is constructed, and have compensated by sort-of "hacking" the FooBar object to take matters into its' own hands.

This is the result of a failure to adhere to another tenent of object oriented programming (in the case of Bar,) which is that an object's state should be fully initialized and ready to handle any incoming calls to its' public members immediately after creation. Now, this does not mean immediately after the constructor is called in all instances. When you have an object that has many complex construction scenarios, then it is better to expose setters to its optional members to an object that is implemented in accordance with a creation design-pattern (Factory, Builder, etc...) In any of the latter cases, you would be pushing the initialization of the target object off into another object graph whose sole purpose is directing traffic to get you to a point where you have a valid instance of that which you are requesting - and the product should not be considered "ready" until after this creation object has served it up.

In your example, the Bar's "status" property does not seem to be in a valid state in which a FooBar can accept it - so the FooBar does something to it to correct that issue.

The second issue I am seeing is that it appears that you are trying to test your code rather than practice test-driven development. This is definitely my own opinion at this point in time; but, this type of testing is really an anti-pattern. What you end up doing is falling into the trap of realizing that you have core design problems that prevent your code from being testable after the fact, rather than writing the tests you need and subsequently programming to the tests. Either way you come at the problem, you should still end up with the same number of tests and lines of code had you truly achieved a SOLID implementation. So - why try and reverse engineer your way into testable code when you can just address the matter at the onset of your development efforts?

Had you done that, then you would have realized much earlier on that you were going to have to write some rather icky code in order to test against your design and would have had the opportunity early on to realign your approach by shifting behavior to implementations that are easily testable.


P
Pavlo

I agree with @toskv: I wouldn't recommend to do that:-)

But if you really want to test your private method, you can be aware that the corresponding code for the TypeScript correspond to a method of the constructor function prototype. This means that it can be used at runtime (whereas you will probably have some compilation errors).

For example:

export class FooBar {
  private _status: number;

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

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

will be transpiled into:

(function(System) {(function(__moduleName){System.register([], function(exports_1, context_1) {
  "use strict";
  var __moduleName = context_1 && context_1.id;
  var FooBar;
  return {
    setters:[],
    execute: function() {
      FooBar = (function () {
        function FooBar(foo) {
          this.foo = foo;
          this.initFooBar({});
        }
        FooBar.prototype.initFooBar = function (data) {
          this.foo.bar(data);
          this._status = this.foo.foo();
        };
        return FooBar;
      }());
      exports_1("FooBar", FooBar);
    }
  }
})(System);

See this plunkr: https://plnkr.co/edit/calJCF?p=preview.


D
Devpool

The answer by Aaron is the best and is working for me :) I would vote it up but sadly I can't (missing reputation).

I've to say testing private methods is the only way to use them and have clean code on the other side.

For example:

class Something {
  save(){
    const data = this.getAllUserData()
    if (this.validate(data))
      this.sendRequest(data)
  }
  private getAllUserData () {...}
  private validate(data) {...}
  private sendRequest(data) {...}
}

It' makes a lot of sense to not test all these methods at once because we would need to mock out those private methods, which we can't mock out because we can't access them. This means we need a lot of configuration for a unit test to test this as a whole.

This said the best way to test the method above with all dependencies is an end to end test, because here an integration test is needed, but the E2E test won't help you if you are practicing TDD (Test Driven Development), but testing any method will.


S
Sani Yusuf

This route I take is one where I create functions outside the class and assign the function to my private method.

export class MyClass {
  private _myPrivateFunction = someFunctionThatCanBeTested;
}

function someFunctionThatCanBeTested() {
  //This Is Testable
}

Now I don't know what type of OOP rules I am breaking, but to answer the question, this is how I test private methods. I welcome anyone to advise on Pros & Cons of this.