4 ms·
Exactly - we shouldn’t be overly concerned with whether the method is public or private. Instead we should be concerned with things like: Are these tests going
by freetime2 5y ago
Exactly - we shouldn’t be overly concerned with whether the method is public or private. Instead we should be concerned with things like:
Are these tests going fail any time someone makes the slightest change to the implementation?
Do these tests require a ridiculous amount of setup?
Are these tests going to run so slowly that they adversely impact the velocity of the entire team?
I agree that as a general rule it’s preferable not to test private methods directly. But if testing a private method is demonstrably more efficient than getting at it through public methods, and the tests don’t end up being too brittle, then that’s probably what you should do. You could also take a look at refactoring the code to make it easier to test - but again only if that actually improves things.
- withinboredom 5y agoI look at it like this: what are the inputs/outputs of what I’m testing? Is it calling another library to do work, spy on that. Is making side-effects in the system, watch that. The only time I’d use internal/private methods directly in a test is when setting up to use the public methods would be tremendous (imagine configuring a database driver to fail in a certain way, for example). Most of the time, I see private methods being tested when the library is “in the middle” of other libraries, or is not properly abstracted.