Written August 17, 2010 at 07:00 MDT Tagged tools, ruby and bdd
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.
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):
Assuming an installed ruby version of 1.8.7, here are the steps from the command line:
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:
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.
#!/usr/bin/env ruby
AUTOTEST_IMAGE_PATH = File.dirname(File.symlink?(__FILE__) ? File.readlink(__FILE__) : File.expand_path(__FILE__))
MATCH_PATTERN = /\d+\s.*examples?,\s\d+\s.*failures?(?:,\s\d+\s.*pending)?/
module Autotest::Growl
def self.display(results,image)
growl "Spec Results", "#{results}", File.join(AUTOTEST_IMAGE_PATH,"#{image}.jpg")
end
def self.get_result_image(output)
if output =~ /[1-9]\sfailures?/ || output =~ /errors/
"red"
elsif output =~ /pending/
"pending"
else
"green"
end
end
Autotest.add_hook :ran_command do |autotest|
filtered = autotest.results.grep(/\d+\s.*examples?/)
output = filtered.empty? ? "errors" : filtered.last.slice(MATCH_PATTERN)
display(output,get_result_image(output))
end
private
def self.growl (title, message, image)
system "growlnotify -n autotest --image #{image} -m #{message.inspect} #{title}"
end
end
Autotest.add_hook :initialize do |autotest|
%w{.svn .hg .git vendor}.each {|exception| autotest.add_exception(exception)}
end
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:
~/repositories/developwithpassion/devtools/mac/spec_growl/
This also allows me to put in custom images for the files:
Now you are ready to hit the ground running and start writing some tests!
The following screencasts demonstrates how it all works:
AutoSpec Demo from Jean-Paul Boodhoo on Vimeo.