2 ms·
Based on my experiences, I don't think anything should be truly private. I found it's almost always better to put the "private" code in a separate "implementati
by magicalhippo 5y ago
Based on my experiences, I don't think anything should be truly private. I found it's almost always better to put the "private" code in a separate "implementation detail" namespace that makes it clear it's intended to be private.
This hides the private implementation details, but keeps them accessible for when it's needed for internal tests or workarounds. It's easy to find code which relies on the private namespace, and one can't accidentally use it.
- hinkley 5y agoIn Java I would sometimes make them protected methods and use a subclass at the top of the suite to expose them. These tests that cross from black to white box testing could be construed as functional tests, in which case my solution and yours aren’t so different. Also important to remember that it’s a mistake to treat library and application code as the same thing. They are not the same, and from my observations a lot of arguments about The Right Way come down to trying to treat them as if they are.
- magicalhippo 5y agoIndeed protected methods can be useful for this. Depending on the task at hand, I find it can be useful to separate code into it's own namespace (one way or another) to make it more clear what the user-facing code does. Regardless, as you noted, my point is to not prevent access but rather make it obvious if you're using implementation details.
- hinkley 5y agoMost test frameworks are very un-opinionated, and perhaps that is a mistake. Framework doesn't care if half your unit tests are functional tests, if you slide E2E test code into the before*() call in an integration test. If your white box and black box tests are snuggled up to each other in the same test suite. If your tests only pass if they run sequentially, in this particular order. It's no wonder we can't get the terminology straight. You can be years into writing tests before anyone or anything calls you out.
- travisjungroth 5y ago> Also important to remember that it’s a mistake to treat library and application code as the same thing. They have the same pattern at two different levels. What’s the essence of public? It’s that there’s an encouragement for people to use it and you don’t know who is using it. Private is the opposite. Its usage is discouraged or not allowed beyond a certain boundary, so you know everyone using it in an endorsed way. In a library, your public methods are public and your private methods are private. In a service, all of your code is private! Critically, it meets the definition that you can see all of the usage. So, why do we test public methods in services? (I’m sure someone would say not to). We just want to be more confident they work as expected now and will continue to do so in the future (I think people forget that’s a [the, even] big reason for tests). Sometimes testing something “more private” gives you faster, more accurate tests. Maybe you can see it coming that I’ll argue by analogy it can also make sense to test private methods in libraries. They’re not part of your external API. But, just like public methods in a service are an internal API, private methods are part of an internal API for the class that is used by your future self and coauthors.
- magicalhippo 5y ago> private methods are part of an internal API for the class that is used by your future self and coauthors Indeed. In a good code, one could say it's libraries all the way down. For example, I've refactored a lot of business code such that it can be easily re-used in different parts of our application. Ta-da, I effectively have created a piece of library code, and I'll want to write tests for it to make sure it doesn't break any consumers, current or future.