3 ms·
> Unit tests, as evident by the name, revolve around the concept of a “unit”, which denotes a very small isolated part of a larger system. There is no formal de
by kccqzy 6y ago
> Unit tests, as evident by the name, revolve around the concept of a “unit”, which denotes a very small isolated part of a larger system. There is no formal definition of what a unit is or how small it should be, but it’s mostly accepted that it corresponds to an individual function of a module (or method of an object).
Heh I feel like this is the crux of the issue. Because there's no standardized definition of what a unit is, people sometimes tend to choose the wrong unit to test.
I personally believe it is usually wrong to test a single function or method in a class. I tend to test the behavior of a whole class at the same time. Testing each individual method is too white-box, and makes your testing code too coupled to the implementation. Basically, don't test internal details (unless the internals are very complicated), just test the externally visible behavior, which is usually presented as a whole class.
I also don't agree with always mocking out dependencies. If your "dependency" is just an instance of a different class, then just use the real deal. Sure, now your definition of a unit now encompasses not just your code, but your dependency's code, but if your dependency's code affects the externally visible behavior of your code, it remains your responsibility if your dependency changes things and breaks your code. That's what abstraction means: you present an interface, your client doesn't need to care about how it's implemented, and what dependency your code needs.