Obama '08
New-formula-starburst

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

2

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.

Comments

  • Avatar Paul said about 1 month later:

    Thanks for your post.

    Re note #4: I think the main reason that people hook up autospec to Growl is to avoid having to switch screens all the time. If I switch from my editor to the terminal window running autospec every time I save my code, that is a lot of extra keystrokes. If I have growl running, then I only have to save.

    With the number of red-green cycles I go through in a day, I probably save 15-30 minutes of time by having growl-like notifications for autospec.

  • Avatar MattyB said 2 months later:

    Firstly, a very useful article. Thank you.

    Secondly, I would like to take up your gracious invitation to debate the merits of using growl (in my mac-deprived case, mumbles) with autospec.

    Yes, it’s gimmicky.

    Yes, I can see why you might say it is lame.

    But above all, yes, I’m a geek.

    Thus the appeal. As one of the people who could be classed as an early adopter of Beryl (now compiz fusion) flashy graphics and useless animations make me smile. Of course, I use the terminal to start autospec anyway. Why should I need to go back to that terminal to see the results? And hey, it’s a million times more useful than holding down Ctrl-Alt-Right and watching my desktops spin in the form of a cube for hours on end.

    (Disclaimer: Of course, the only reason I installed beryl was purely for the usability and thus productivity benefits it provided. I would never dream of sitting there for more than about half an hour watching my desktop spin…)

Trackbacks

Use the following link to trackback from your own site:
/articles/trackback/19499

(leave url/email »)

   Comment Markup Help Preview comment