Monday, January 29, 2007

RSpec does Autotest

I finally got around to updating to the latest version of RSpec and got it working with Rails and Autotest thanks to a helpful post by Robby On Rails.

But there were a couple of little things that tripped me up:
  1. You must match the versions of the rspec_for_rails plugin with your version of RSpec.
  2. The path structure for has changed from the previous paths. It's now found at svn://

    (Don't know if this is a bug or not...)
  3. The API has changed to align should_eql/should_equal with ruby's eql?/equal? - a bit different than what I'm used to with Test::Unit, but I suppose more Rubyish. (more info)

Also, running autotest against the spec server really helps with performance, which is key when you're doing test-first development.

Here's how you can automate that in a rake task:
  1. Add your own task at lib/tasks/rspec_autotest.rake (I put it here so it doesn't get clobbered when I update the plugin):

    namespace :spec do

    task :autotest_server do
    require File.join(File.dirname(__FILE__),
    'lib', 'rspec_autotest')
    autotest =
    autotest.spec_command = "drbspec --diff unified"

  2. Kick off the spec server:
  3. And start up autotest:
     rake spec:autotest_server


Oh, and here's how I got autotest color to work with rspec. (Thanks for asking, Scott!)

First, I added this to ~/.autotest to load redgreen which comes with the lastest version of ZenTest.

require 'autotest/redgreen'

Second, I updated my copy of redgreen.rb at /opt/local/lib/ruby/gems/1.8/gems/ZenTest-3.4.3/lib/autotest/redgreen.rb
(your location may vary)

module Autotest::RedGreen
BAR = "=" * 80
RED = 31
GREEN = 32

Autotest.add_hook :ran_command do |at|
if at.results.match /specification/
at.results.gsub!(/^.* specifications?, (\d+) failures?$/) { |match|
code = ($1 != "0") ? RED : GREEN
match_string(code, BAR, match)
at.results.gsub!(/^.* (\d+) failures, (\d+) errors$/) { |match|
code = ($1 != "0" or $2 != "0") ? RED : GREEN
match_string(code, BAR, match)


def self.match_string(code, bar, match)


Blogger Scott said...

Thanks so much. I was looking for a solution which was better than mine, in which I had to modify the plugin for initialize to take an argument. Great tip!

4:19 PM  
Blogger Scott said...

Any idea how to use drbspec with color? Passing the --color option doesn't seem to work.

4:22 PM  
Blogger victorcosby said...


Check my original post for an update on how I got rspec and autotest to work with redgreen.


6:32 PM  

Post a Comment

Links to this post:

Create a Link

<< Home