Practical Web Programming

Wednesday, October 23, 2013

Automatically Run Tests with Guard

Step by step guide:

1) Edit the Gemfile:
    group :development, :test do
      gem 'guard-rspec', '1.2.1'
    # Add System-dependent gems
    group :test do
      gem 'capybara', '1.1.2'
      # Test gems on Macintosh OS X
      gem 'rb-fsevent', '0.9.1', :require => false
      gem 'growl', '1.0.3'
      # Test gems on Linux
      gem 'rb-inotify', '0.8.8'
      gem 'libnotify', '0.5.9'
      # Test gems on Windows
      gem 'rb-fchange', '0.0.5'
      gem 'rb-notifu', '0.0.4'
      gem 'win32console', '1.3.0'

2) Run the bundler.
    $ bundle install

3) Initialize Guard so that it works with RSpec.
    $ bundle exec guard init rspec

4) Edit the resulting Guardfile so that Guard will run the right tests when the integration tests and views are updated
    require 'active_support/core_ext'

    guard 'rspec', :version => 2, :all_after_pass => false do  # Ensures that Guard doesn’t run all the tests after a failing test (to speed up the Red-Green-Refactor cycle).
      watch(%r{^app/controllers/(.+)_(controller)\.rb$})  do |m|
         (m[1][/_pages/] ? "spec/requests/#{m[1]}_spec.rb" : 
      watch(%r{^app/views/(.+)/}) do |m|
        (m[1][/_pages/] ? "spec/requests/#{m[1]}_spec.rb" : 

5) Start guard
    $ bundle exec guard

To clear the screen everytime guard runs, add -c parameter. Ex: guard -c
If you get a Guard error complaining about the absence of a spec/routing directory, you can fix it by creating an empty one: $ mkdir spec/routing


Recent Post