Use Autospec to Your Advantage

July 5, 2009 Pivotal Labs

I like Autotest because it allows me to stay within my code editor and let my test suite run automatically in the background. After each run I get a nice, unobtrusive, growl notification informing me whether my most recent change caused the tests to fail or pass.

It used to be that setting up and configuring Autotest for the OS X with pretty growl notifications was a nontrivial task. I remember that I spent several hours getting it working a year ago. However, there has been several developments with Autotest in recent months that greatly simplify the process. One of those enhancements is that Autotest can now use FSEvent introduced in OS X Leopard so that it no longer has to continuously poll the filesystem. This has the advantage of vastly reducing Autotest’s CPU usage when idle.

Install Autotest

To setup Autotest on a Mac I recommend following this nice, up-to-date, walkthrough on the Viget Labs Blog.

Using Autotest with RSpec

Complete instructions for Autotest integration with RSpec are described on the RSpec wiki. The gist of it is this:

Add an initialize hook to Autotest

Place the following code snippet in a file called .autotest in the root of your Rails project:

Autotest.add_hook(:initialize) {|at|
  at.add_exception %r{^.git}  # ignore Version Control System
  at.add_exception %r{^./tmp}  # ignore temp files, lest autotest will run again, and again...
  #  at.clear_mappings         # take out the default (test/test*rb)
  at.add_mapping(%r{^lib/.*.rb$}) {|f, _|
    Dir['spec/**/*.rb']
  }
  nil
}

Start Autospec in Terminal

RUBYLIB=./lib RUBYOPT=-rubygems AUTOFEATURE=true autospec

To avoid typing or remembering this cumbersome set of options to start autospec, I added it as an alias to my bash environment.

alias autospec='RUBYLIB=./lib RUBYOPT=-rubygems AUTOFEATURE=true autospec'

Autotest Alternative

For using Autotest with RSpec there is also the RSpactor gem which touts advantages homogeneous to those provided by the new autotest-fsevent. I have not tried RSpactor yet. If you do be sure to check out the RSpactor wiki as there is no README on Github.

Notes

  • I do not recommend using Autotest/Autospec with RubyMine. The auto save feature of RubyMine would cause Autotest to run incessantly.
  • Sometimes autospec seems to hang after running for a while and stops detecting that a file has changed. When this happens I send a SIGINT to the process by pressing CTRL+C. This causes autospec to reload and run the entire test suite.
  • To kill autospec press CTRL+C twice.

About the Author

Biography

More Content by Pivotal Labs
Previous
Introducing ActiveApi – A sane way to translate your data to xml
Introducing ActiveApi – A sane way to translate your data to xml

ActiveApi allows you to define a schema in Ruby, and use that schema to convert ruby objects to xml. An ex...

Next
Tweed: Preview of v0.9.8
Tweed: Preview of v0.9.8

Bug Fixes false notification reports timeline marker not being preserved when Load More is tapped loading ...

Enter curious. Exit smarter.

Learn More