Testing Rails 3 with Guard and Zeus

Zeus is a really great tool for speeding up development in Rails 3. It “preloads your Rails app so that your normal development tasks such as console, server, generate, and specs/tests take less than one second.”

Guard responds to changes in your files, making it easy to automate tests.

guard-zeus-client tells Guard to use Zeus commands (e.g. zeus test spec/models/page.rb) so tests run blazingly fast. Awesome.

How to use:

1. Install zeus by typing gem install zeus. I’m running version 0.13.1

Zeus instructs you to install the gem outside of bundler (not in your Gemfile), so that’s what I did.

2. Add these lines to your Gemfile within the :test group:

gem 'guard'
gem 'guard-zeus-client'

3. cd into your project’s root directory and type ‘guard init’. This will create a Guardfile in your project root.

4. edit your Guardfile so Guard watches the correct files, like this:

guard 'zeus-client', :all_on_start => false, :all_after_pass => false do
  ignore %r{^\.zeus\.sock$}

  watch(%r{^lib/(.+)\.rb$}) { |m| "spec/lib/#{m[1]}_spec.rb" }
  watch('spec/spec_helper.rb') { "spec" }

  # Rails example
  watch(%r{^app/(.+)\.rb$}) { |m| "spec/#{m[1]}_spec.rb" }
  watch(%r{^app/(.*)(\.erb|\.haml)$}) { |m| "spec/#{m[1]}#{m[2]}_spec.rb" }
  watch(%r{^app/controllers/(.+)_(controller)\.rb$}) { |m| ["spec/routing/#{m[1]}_routing_spec.rb", "spec/#{m[2]}s/#{m[1]}_#{m[2]}_spec.rb", "spec/acceptance/#{m[1]}_spec.rb"] }
  watch(%r{^spec/support/(.+)\.rb$}) { "spec" }
  watch('config/routes.rb') { "spec/routing" }
  watch('app/controllers/application_controller.rb') { "spec/controllers" }

5. open a terminal window, cd into your project root and start zeus by typing ‘zeus start’, then open another window, cd into you your project root and start guard by typing ‘guard’. you’ll get a warning about using bundler, but you can’t because we installed zeus outside of bundler. Use RVM gemset to make sure you’re running with the correct set of gems.

6. edit a spec, or an associated file, and you should see the test run fast in the terminal window running Guard.


Comments are closed.