Installing RSpec, Autotest, and Spork on Ubuntu/OS X

I'm an Ubuntu lover in the process of learning Ruby on Rails. I've spent a good majority of my time "learning" rails trying to get Rspec, Spork, and Autotest to play nicely. Unfortunately, our designer is an Apple fanboy, so my testing suite needs to perform on OS X as well. It's extremely frustrating to spend so many hours trying to get these gems to work, just so you can continue on with the tutorial! Thus, to save you all the heartache I endured, here is a definitive guide to integrating Spork and Autotest with Rspec for Ubuntu AND Mac OS X!

Time Required

15 - 30 minutes

The Main Problem: Gemfile Hacks

The main issue with most of the tutorials I've followed is that they require you to list gems such as autotest in Gemfiles, and this can cause a huge array of conflicts between the different operating systems. So our philosophy throughout this tutorial is knowing when a gem should be executed via its binary (autotest) or via bundle exec (bundle exec autotest). Most of the time executing via binary or through bundle exec doesn't make any difference. However, in this context, autotest and spork react much differently based upon which way you call them. Thus, in order to keep our project portable, we aim to eliminate any OS-specific hacks in the Gemfile.

Get the Project Template

First, we'll pull a rails project template file that has rspec preconfigured.
$ git clone git://github.com/joequery/Rails-Template.git
Now rename the app to whatever you want
$ rails g rename_to YourApp
Now execute the following
$ bundle install
$ gem install ZenTest
$ gem install autotest-rails
(You may need to use sudo for your gem install, depends on your setup) Now we move on to OS-Specific implementations.

Ubuntu Configuration

(Skip to OS X) Credit most of the Ubuntu instructions to Ykyuen First, execute
$ sudo apt-get install libnotify-bin
Now create an autotest configuration file in the home directory (~/.autotest) and paste in the following: [ruby] #!/bin/ruby #require 'redgreen' require 'autotest/timestamp' module Autotest::GnomeNotify def self.notify title, msg, img system "notify-send '#{title}' '#{msg}' -i #{img} -t 1500" end Autotest.add_hook :ran_command do |at| image_root = "~/.autotest_images" results = [at.results].flatten.join("\n") results.gsub!(/\\e\[\d+m/,'') output = results.slice(/(\d+)\sexamples?,\s(\d+)\sfailures?(,\s(\d+)\spending?|)/) full_sentence, green, failures, garbage, pending = $~.to_a.map(&:to_i) if output if failures > 0 notify "FAIL", "#{output}", "#{image_root}/fail.png" elsif pending > 0 notify "Pending", "#{output}", "#{image_root}/pending.png" else notify "Pass", "#{output}", "#{image_root}/pass.png" end end end end [/ruby] Now create an ~/.autotest_images folder and extract these images to it. Everything is now configured. Jump to Testing Your Setup

Mac OS X

First, execute
$ gem install autotest-growl
(You may need to use sudo, based upon your setup). Edit the ~/.autotest file (create it if it does not exist), and paste the following in
require "autotest/growl"
Everything is now configured. Jump to Testing Your Setup

Testing Your Setup

The Ubuntu Nerds and the Apple Fanboys reunite! It's time to test our installations. First, lets generate a controller to test.
$ rails generate controller Pages home contact
You'll see a bunch of files created. Let's get rid of some of the unnecessary files rspec made
$ rm -rf spec/helpers
$ rm -rf spec/views
Now let's run our test without autotest. We should have 2 success and 0 failures at this point.
$ bundle exec rspec spec/
Now open two terminal windows or tabs and cd back to your project directory in both of them. In one tab, execute
$ bundle exec spork
You may see a bunch of warning messages. It's safe to ignore these for the time being. If everything went well, you should see
Preloading Rails environment
Loading Spork.prefork block...
Spork is ready and listening on 8989!
In the other tab you opened, simply execute
$ autotest -c
Once again, you may see a bunch of warning messages in the terminal, and these are also safe to ignore for now. More importantly, you should see a pop up notification that shows that the tests passed. You'll also see
Finished in 0.xxxx seconds
2 examples, 0 failures
in the window/tab where you executed autotest. Now, to see if the autotest is working properly, lets open spec/controllers/pages_controller_spec.rb. The file contains the following: [ruby] require 'spec_helper' describe PagesController do describe "GET 'home'" do it "should be successful" do get 'home' response.should be_success end end describe "GET 'contact'" do it "should be successful" do get 'contact' response.should be_success end end end [/ruby] Lets make the test fail by changing all occurances of "contact" in the second describe block to "about". [ruby] require 'spec_helper' describe PagesController do describe "GET 'home'" do it "should be successful" do get 'home' response.should be_success end end describe "GET 'about'" do it "should be successful" do get 'about' response.should be_success end end end [/ruby] We should get a pop up saying that the test failed, and you can find the details in the tab with autotest running.
Finished in 0.07293 seconds
2 examples, 1 failure

Failed examples:

rspec ./spec/controllers/pages_controller_spec.rb:13 # PagesController GET 'about' should be successful

# Waiting since 2011-09-13 14:19:31

If this shows up along with the pop up, congratulations, you've successufully integrated Spork and Autotest into RSpec! Note: Sometimes spork can get kind of screwy. If you're ever in a situation where you KNOW a test should pass or should fail, you should restart spork and see if that helps.

Do I have to do this EVERY TIME?!?!

Nope! For subsequent projects, your procedure should be as follows:
$ git clone git@github.com:joequery/Rails-Template.git
$ cd Rails-Template
$ rails g rename_to MyApp
$ bundle install
In a separate window/tab
$ bundle exec spork
In another separate window/tab
$ autotest
And that's it!

Feedback

Did the tutorial help? Did you find parts of the tutorial unclear? Let us know in the comments below! September 13, 2011
About the Author:

Joseph is the lead developer of Vert Studios Follow Joseph on Twitter: @Joe_Query
Subscribe to the blog: RSS
Visit Joseph's site: joequery.me