3 ms·
Tests are not about bugs. Tests are about code correctness. If applying tests, which I personally do while I write my production code, is catching bugs then tha
by danielovichdk 3y ago
Tests are not about bugs. Tests are about code correctness. If applying tests, which I personally do while I write my production code, is catching bugs then that is of course a positive side effect. I write classic unit tests for the code where it's possible but do I have external dependencies I either mock them or stub them.
While writing tests alongside writing production code you might become aware of code that does not conform to your own qualitative measurements. SRP, DRY etc. That process, writing tests while you write porduction code, imo, has a tendency to better your design. But I never look at design as being done, I look at it as being good enough for the time being.
If you write test after you write you write your production code, bettering your design becomes more difficult, since the code is already written and you might need to make larger changes that will adhere to how you really wanted the code to look like.
Tests should give you confidence. Without them you are clueless and have no confidence when you start making changes. You cannot juggle with, on a cognitive level, that if you apply a change to your code, that whings will just work. Tests gives you that confidence because it gives you fast feedback.
You can not argue that removing dependencies and therefor mocking is a possibility. Unless of course you have no dependencies, like the calculatur or greetings funtion in the post.
You can move your dependencies around and perhaps invert them, but you cannot remove them. A good example is that instead of injecting external dependencies to your own classes, inject objects which represent a dependency in isolation.
Then you can control the isolated object which is a representation of an external dependency but do it without mocking.
The best book I have read about testing is Vladimir Khorikov's https://www.amazon.com/gp/product/1617296279/ref=as_li_tl?ie=UTF8&camp=1789&creative=9325&creativeASIN=1617296279&linkCode=as2&tag=vkhorikov-20&linkId=2081de4b1cb7564cb9f95526533c3dae https://www.amazon.com/gp/product/1617296279/ref=as_li_tl?ie...
- danielovichdk 3y agoI forgot to mention that writing tests has, imo, a lot to do about thinking about what production code you write. https://fs.blog/writing-to-think/ https://fs.blog/writing-to-think/