Getting started with Unit Testing #1: Start fresh!
A lot of developers who embark on the Unit Testing journey will go:
-"Okay, I'll write a test in my current project just to see how it feels!".
This is almost always a bad idea and chances are you’re going to get stuck and give up. And here's why:
Your code is not testable!
Most developers think that they are writing testable code even though they haven’t written a single test. I'm sorry, but this is almost never true.
I have never stumbled across "testable code without tests" that I’ve been able to write a test for without first having to rewrite most of the so called "testable code".
So if you start of by trying to create a test for legacy code you might focus too much time and effort on getting that test to pass when it’s actually nothing wrong with your test.
It’s your code that needs to be rewritten, and chances are you aren’t interested in rewriting legacy code that works just to get a test to pass.
So do yourself a favour: Start fresh! Decide that your next project (or isolated feature within your current project) is going to be the start of your Unit Testing journey.
Cheers friends! ❤️
ps. The # in the title is because I intend to do a little series of these "Getting started with Unit Testing" posts over the upcoming weeks.