Obama '08
New-formula-starburst

autotest is now autospec - How to Set Up Autospec for RSpec and Rails with ZenTest

1

Automatically running your specs as you write code is a very effective way to improve your Behavior Driven Developent (BDD) practices and overall productivity. When I’m building new features in my Rails apps, I keep one terminal window in view with autospec running continuously, detecting changes to my files, and automatically running all relevant specs that the file may affect. It gives me nearly instant feedback about any change that I made that has completed a pending feature, or may have broken something inadvertently.

If you’re struggling with sticking with BDD, try setting up autospec and spend a few hours trying my method:

I’m assuming that you already have RSpec up and running and know how to write specs (as of writing this, I’m using RSpec 1.1.4). If not, here’s a good introduction to Rspec.

Install required gems

sudo gem install ZenTest

Create a .autotest file

In your home directory create a file named .autotest and paste the following:

Autotest.add_hook :initialize do |at|
  %w{.svn .hg .git vendor}.each {|exception| at.add_exception(exception)}
end

This will tell autotest to ignore the SVN and Git hidden files within your code.

Write specs. Make it green. Repeat.

Now you’re ready to code furiously! Launch autospec in a new terminal window from your Rails project root. At first, autospec will run through all your specs just like rake spec, but will then wait. As soon as it detects changes to a file in your application, autospec will figure out what specs to run and automatically execute them.

Try this best practice to fully realize the benefits and pleasure of BDD:

1. With autospec running, write a few specs that describe a feature or requirement that you want to implement. For a simple example, let’s assume you have just generated a Rating model. The requirement is that it has a score between 1 and 10. Open up spec/models/rating_spec.rb and …

describe Rating do

  before(:each) do
    @rating = Rating.new
  end

  it "should not have a score less than 1" do
    @rating.score = 0
    @rating.should have_exactly(1).error_on(:score)
  end

  it "should not have a score greater than 10" do
    @rating.score = 11
    @rating.should have_exactly(1).error_on(:score)
  end

  (1..10).each do |i|
    it "should be valid with a score of #{i}" do
      @rating.score = i
      @rating.should be_valid
    end
  end
end

2. Now as soon as you save this file in rating_spec.rb, autospec will be painted red with failures. Let’s implement this requirement in app/models/rating.rb:

class Rating < ActiveRecord::Base
  validates_inclusion_of :score, :in => 1..10
end

3. Save the file, and glance over to your autospec screen which should now be green. Congratulations, you just succeeded at behavior driven development! Now repeat this process by writing a few specs for a single feature or requirement first, then switch over to your actual models or controllers and make them pass. Keep it green, keep it clean.

Notes about autospec

  1. Autospec won’t pick up changes to your database that you make via migrations. If you change the database structure at all, exit autospec by hitting CTRL-C twice, then run rake spec to reload your test database. Then you can start autospec again.
  2. Autospec also won’t pick up new spec files while it is running. Just exit and start autospec again.
  3. After a spec file with failures has gone all-green, autospec will re-run the entire spec suite as a regression.
  4. There are ways that people have hooked up autospec to Growl notifications. I think this is lame and gimmicky. You’re a geek. Use the terminal.