ZenTest 3.5 + RSpec 0.9 = AutoSpec
One thing I find with developers who are new to writing tests/specs is that they will often forget to run the test suite prior to checking in their changes. When you’re developing by yourself this is one thing, but when developing on a large team this can often cause shame and ridicule when you break the build. Ryan Davis has attempted to solve this problem which a great tool called autotest, which is part of the ZenTest suite of tools.
Autotest is a script which runs in the background monitoring your source directory, automatically running your test suite when it notices a file has been modified. This is a huge productivity gain as it allows you to focus on writing code and not have to worry about switching contexts to run your tests/specs.
Prior to the latest version you were required to install an additional plugin if you wanted to use autotest on your Rails project with RSpec (you are writing specs right?). Thankfully version 3.5 of ZenTest now includes a RSpecRailsAutotest runner which will monitor for changes in your Rails source and run the appropriate specs.
Unfortunately this first pass at RSpec support assumes that you have the gem installed, and doesn’t work if you’ve decided to include rspec and rspec_on_rails into your vendor/plugins directory. Thankfully due to the extensible nature of autotest this problem is easily fixed by adding the following to your ~/.autotest file:
1 2 3 4 5 6 |
Autotest.add_hook :initialize do |autotest| if autotest.is_a? RspecRailsAutotest autotest.spec_command = 'script/spec --options spec/spec.opts' if File.exist? 'script/spec' end end |
This will force autotest to use the spec command that is installed within the script/ directory of your Rails project as well as have it use the options defined in spec/spec.opts.
Note that this assumes that you are using RSpec version 0.9 which includes the script/spec command. If not you’ll need to modify the spec_command line above to point towards vendor/plugins/rspec/bin/spec.
Happy (Auto)Specing!

Comments
Jean-Michel Garnier says:
April 17, 2007 @ 09:01 AM
Thanks for the trick Josh! I have also posted some instructions to install & configure rspec 0.9.0 beta2 + ZenTest 3.5 on my blog:
http://21croissants.blogspot.com/2007/04/set-up-rspec-on-rails.html
Ryan Davis says:
May 30, 2007 @ 07:48 AM
This is all out of date now that rspec 1.0.x and zentest 3.6.x is out.