Do you want to quickly get going with tdd in ruby? Here is the simplest rundown to get a working effective tool stack that will help you start coding more effectively in a test driven fashion in your ruby environment. I am not even discussing rails here at the moment, along with all of the tools that it brings to the table for testing. Just core ruby.
Basic Requirements
For this post I am assuming an osx based environment, mostly for the growl ability. All of the other tools that are mentioned will work without issue on a regular unix based environment (I have not tried this stuff on the windows platform yet):
- Ruby – Need I say more
- Growl – install this so that you will later on be able to use growl notifications (think toast windows).
- RVM – Ruby Virtual Machine. This is a great tools to keep you base ruby installation completely clean, as well as be able to install/test multiple versions of ruby against your application. You don’t need to install this, but I would strongly recommend it.
Gems
Assuming an installed ruby version of 1.8.7, here are the steps from the command line:
- gem install rspec
- gem install autotest
- gem install autotest-growl
- gem install autotest-fsevent
That’s about it for the gems you need to install. Autotest is a tool that you can use to monitor your working folder in the background and it will automatically rerun the accompanying tests if there are changes to any of the items under lib/specs directory. RSpec has a thin wrapper around autotest called autospec. It has a default convention that it uses to monitor for file changes in your application. With the default convention, you just need to make sure that:
- your test code lives under a folder named spec
- your production code is in the lib folder
The last pieces of the puzzle is to hook into autotest to do the growl notification. I created a folder under my devtools folder named spec_growl add a simple .autotest file to your root directory that contains a couple of hooks into the AutoTest, I took a script that I found at this location and just modified it slightly.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 | |
Either save that file to your home folder (~) with the name ~/.autotest. Or create a symlink to the file with a name of ~/.autotest. I have it all located in a folder under my devtools folder:
1
| |
This also allows me to put in custom images for the files:
- red.jpg
- green.jpg
- pending.jpg
Now you are ready to hit the ground running and start writing some tests!
Screencast
The following screencasts demonstrates how it all works:
AutoSpec Demo from Jean-Paul Boodhoo on Vimeo.