Quickly Setting Up Autospec and Growl for TDD Goodness in Ruby!!

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
#!/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:

bash
1
~/repositories/developwithpassion/devtools/mac/spec_growl/

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.

Develop With Passion®

Comments