Test Driven Development (TDD)
Now that I actually have some tools to practice TDD, I find it much easier to write and debug a program!
into my code. I would then run the script and hope for:
This was quite a pain because every time I changed the program, I would have to run it again to see if it still worked (assuming it was working before).
In a sense, I was doing test driven development, but in a very crude and amateur way.
So what is Test Driven Development for real?
Using rspec and guard-rspec, which are ruby gems, you can write your own test to check you code as you write it. Using the example above, I could write a primes_spec.rb file (by convention, spec files are placed in a spec/lib/ directory, and should match the lib/ directory at your project root)
If you are running guard in your terminal (I’ll assume you know how to install gems, and figure out how to run guard), whenever you make a change to your primes.rb code, guard will automatically run those tests and give you a pass or fail status.
Now, there is more to TDD than just writing code and writing tests for it. There is a methodology to the process, called ‘Red, Green, Refactor’ which I will discuss in the next blog post.