05 June 2007

Set up rspec on Rails

I have described the steps to install and configure rspec, the Autotest module from ZenTest and a notifier for Ubuntu.

Make sure you have the latest rubygems (0.9.4)

sudo gem update --system

Installing  rspec

At the time of this writing - 5th of June 2007 - latest version of rspec is 1.0.4, and we'll install it in the vendor/plugins:

cd <your project home>
ruby script/plugin install svn://rubyforge.org/var/svn/rspec/tags/CURRENT/rspec
ruby script/plugin install svn://rubyforge.org/var/svn/rspec/tags/CURRENT/rspec_on_rail
Check out http://rspec.rubyforge.org/ for updates (I could not find a RSS) but I guess Aslak will be updates on his blog.

Warning: if you had installed rspec before, run a "gem list" to check if you have a web_spec rogue gem, you must delete it before carrying on, more details in Zenspider's blog.

Installing and configuring Zentest

Latest version of ZenTest is 3.6.0, run
sudo gem install ZenTest
Thankfully due to the extensible nature of autotest, we'll be able to set up a notifier for ubuntu by adding the following to your <Rails project root>/.autotest file:







 

#!/usr/bin/env ruby

module Autotest::GnomeNotify

# Time notification will be displayed before disappearing automatically
EXPIRATION_IN_SECONDS = 4
ERROR_STOCK_ICON = "gtk-dialog-error"
SUCCESS_STOCK_ICON = "gtk-dialog-info"

# Convenience method to send an error notification message
#
# [stock_icon] Stock icon name of icon to display
# [title] Notification message title
# [message] Core message for the notification
def self.notify stock_icon, title, message
options = "-t #{EXPIRATION_IN_SECONDS * 1000} -i #{stock_icon}"
system "notify-send #{options} '#{title}' '#{message}'"
end

Autotest.add_hook :red do |at|
notify ERROR_STOCK_ICON, "Tests failed", "#{at.files_to_test.size} tests failed"
end

Autotest.add_hook :green do |at|
notify SUCCESS_STOCK_ICON, "All tests passed, good job!", ""
end

end

# a pop up window will appear to display the tests results
# More examples in /usr/lib/ruby/gems/1.8/gems/zentest-3.5.0/example_dot_autotest.rb

# see http://ph7spot.com/articles/getting_started_with_autotest

The GnomeNotify was written by Philippe Hanrigou, a French Thoughtworker living in California, he has written the best "getting started" guide to Autotest. To install libnotify on ubuntu, run:
sudo apt-get install libnotify-bin

Finally, from the Rails project’s root, type:

autotest


Pressing Control-C twice in quick succession will exit Autotest back to the shell prompt.


Integration with netbeans


As far as I am concerned, netbeans ruby-ide is the first IDE to integrate autotest. Select your project root in the Projects windows, right click and then "Auto Test". This will open an output window and run Autotest. This is very cool, check out the screenshot:






1 comment:

Anonymous said...

For ZenTest 3.9.2, it seems that "at.files_to_test.size" returns the number of files that failed, not the number of tests that failed. The following snippet seems to work for me.

notify ERROR_STOCK_ICON, "Tests failed", "#{at.files_to_test.values.flatten.size} tests in #{at.files_to_test.size} files failed"