Ruby on Rails BDD with Autotest, Growl, and Rspec
Published: May 14th, 2007If you are utilizing Rails, BDD, and Textmate, well, here’s an awesome productivity tip!
Alright so you happened to have fallen upon the idea of BDD (Behavior Driven Development) and also chose to use the Rspec on Rails testing framework to utilize the BDD philosophy. This is absolutely great! Now, all you need to do is understand that this can get even cooler.
What we are going to allow here is to have autotest(part of the zentest library) running in the background to notify us if our tests are passing or failing. The beauty about this, is that a growl notifier will pop up with a beautiful display of what is going on. Example:
Make sure you have all these requirements installed:
Growl
Zentest
Rspec
Then edit your .autotest file in the home directory of your computer. If it doesn’t already exist; create one.
sparta@phalanx sparta $ mate ~/.autotest
(mate is a command line operation to open the specified path in Textmate; an excellent text-editing program for OS X)
Put this in there:
module Autotest::Growl def self.growl title, msg, img, pri=0, stick="" system "growlnotify -n autotest --image #{img} -p #{pri} -m #{ msg.inspect} #{title} #{stick}" end Autotest.add_hook :ran_command do |autotest| output = autotest.results.last.slice(/(\d+)\s.*examples?,\s(\d+)\s.*failures?/) if output =~ /[1-9]\sfailures?/ growl "Test Results", "#{output}", "~/Library/autotest/rails_fail.png", 2, "-s" else growl "Test Results", "#{output}", "~/Library/autotest/rails_ok.png" end end end
Make sure the image path to the red and green Rails icons are proper. You may download the images at the end of this post.
Then bam, you should have a sexy setup to get down and dirty with programming, in the coolest way possible.





Hey Dan,
i think there’s a mistake in the regular expression somewhere… I’m getting green when tests are failing. When I decipher the expression I’ll write you with the fix :)
Try this:
module Autotest::Growl
def self.growl title, msg, img, pri=0, stick=”"
system “growlnotify -n autotest –image #{img} -p #{pri} -m #{ msg.inspect} #{title} #{stick}”
end
Autotest.add_hook :ran_command do |autotest|
output = autotest.results.last.slice(/(\d+)\s.*examples?,\s(\d+)\s.*failures?/)
if output =~ /[1-9]\sfailures?/
growl “Test Results”, “#{output}”, “~/Library/autotest/rails_fail.png”, 2, “-s”
else
growl “Test Results”, “#{output}”, “~/Library/autotest/rails_ok.png”
end
end
end
[...] 1.0.8 is a new version, we decided to try again. So far it’s worked. So with a big thanks to Daniel Fischer, I’d like to write up [...]
Awesome Dan! Now I have no more excuses on why a build failed cause I didn’t check my autotest window. :)
I made a minor change to your script so I can view my pending as well.
output = autotest.results.last.slice(/(\d+)\s.*examples?,\s(\d+)\s.*failures?,\s(\d+)\s.*pending?/)
Ready. Set. Go.
In terms of the formatting, you're allowed to use markdown, textile, or basic html; it's truly up to you -- what strikes your fancy?
You don't have to worry about your e-mail address being sold to a russian-spam-mafia. I'm only going to use it for my own weird needs; like asking you out for a date on a lonely night of coding.