1

I've built a kinda Queue manager which works with RxJs observables and executes them one by one. Now I want to test this, however all the methods that I want to test are private.

Public interface has only two methods that create an observable and add it to the queue manager.

If I try to make something like spyOn(myService, 'privateMethod') PhpStorm analysis says that it is not assignable to type (and refers to public methods).

I can't extract this into something where these would become public because it's a complicated logic which should not be interrupted from the outside.

4
  • 1
    Dup : stackoverflow.com/questions/35987055/… Commented Feb 20, 2019 at 9:59
  • frankly @trichetriche the link you claim to be a duplicate answers the question in a sense of testing. However if OP want to add a spy on them it does not show how. Commented Feb 20, 2019 at 10:05
  • @NormundsKalnberzins I've undeleted my answer so that he can see it, it should be enough. Commented Feb 20, 2019 at 10:08
  • @NormundsKalnberzins by the way, the duplicate do explain how to access private members of the class. Commented Feb 20, 2019 at 10:09

1 Answer 1

6

You can use spyOn<any>(myService, 'privateMethod') to bypass that.

But if your methods are private, they shouldn't be tested, they should be implementation details. You should instead test the end result of your public functions.

Sign up to request clarification or add additional context in comments.

9 Comments

it's true, "should not be tested". I assume it's a sin, but I've used spies on private methods (like you describe) to mock up a state (say use return value) or verify that certain action has been taken without need to create any more complicated environment these private methods might interact with.
@NormundsKalnberzins instead of mocking the private methods, you should mock the providers (HttpClient most of the times) and leave your private methods as is. I'm talking about best practice here, I'm not forcing you to do it, you do what you want. I'm just telling you what a clean and easy to maintain code looks like.
well requests to HttpClient is the easy one since we have HttpTestController. Otherwise mocking external dependency used by a private method or spying on a private method to override is a matter of syntax/looks not substance
@NormundsKalnberzins it is a matter of substance. Mocking the methods implies that on the slightest change, you have to remock your spies. Mocking the dependency means that if the slightest change occurs, you just have to edit the dependency, and changes would be repercuted all over the tests. Again, I'm not telling you how to code, I'm merely explaining best practices. There's reasons for them being best practices, no matter your arguments, you can't go against them.
The problem is that my private methods cannot become public and are interconnected.
|

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.