I like this. I write tests to cover the happy path and any edge cases I can think of. Once I do this, I examine the code coverage and look for 2 things:
Did I miss an edge case? I generally look for unexecuted catch blocks or branch paths.
Did I really need that code? If there's code that doesn't get run during the tests, and doesn't represent a potential failure, I can remove it. I learn from this, as well. Maybe it was an oversight in thinking through an algorithm, maybe it's an unnecessary bounds check because there's a check at a higher level in the code, etc.
Once I fix the tests and prune, I still only end up with 80-90% coverage. Because why test getters and setters? Things like that that are painfully obvious to reason about don't need a unit test, unless they're doing some kind of complex data mutation. Which they almost never are.
I've been using the Flow type system at work in a large javascript project lately. It was so nice when we first started using it because it felt like it re-enabled us to actually refactor without breaking things. I feel like it or Typescript is pretty much necessary for any javascript project that reaches a certain size (and useful even if they don't get big).
Unit tests should be "light touch". Each "object" should have a Unit tests with stubbed dependencies and 100% coverage. If you're writing your code discreetly enough, you can stub dependent objects and only worry about the logic flow of the object under tests. Each object is the responsible for only its own functionality, and the interface with dependent objects is stubbed to provide expected happy path/error case output.
When you refactor, you guarantee you're still meeting the contract and that the object's control flow is 100% "touched". When refactoring a single object (or suite of objects), you don't end up chasing down 100 tests to fix, because you know exactly which objects you're affecting and don't need to worry about side effects.
Then your functional/integration tests then endure you're application as a whole is still meeting the contract.
52
u/[deleted] May 08 '17 edited Oct 04 '17
deleted 0.3553 What is ^^^this?