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'
    end
    
    # 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'
    end 

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|
        ["spec/routing/#{m[1]}_routing_spec.rb",
         "spec/#{m[2]}s/#{m[1]}_#{m[2]}_spec.rb",
         "spec/acceptance/#{m[1]}_spec.rb",
         (m[1][/_pages/] ? "spec/requests/#{m[1]}_spec.rb" : 
                           "spec/requests/#{m[1].singularize}_pages_spec.rb")]
      end
      watch(%r{^app/views/(.+)/}) do |m|
        (m[1][/_pages/] ? "spec/requests/#{m[1]}_spec.rb" : 
                           "spec/requests/#{m[1].singularize}_pages_spec.rb")
      end
      .
      .
      .
    end

5) Start guard
    $ bundle exec guard

NOTE:
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

Use Factory_Girl to Create Mock Data

Step by step guide"

1) Edit the Gemfile
    group :test do
        .
        .
        .
        gem 'factory_girl_rails', '4.1.0'
    end

2) Install it
    $ bundle install

3) Factory Girl format for Person model
    FactoryGirl.define do
      factory :person do
        name     "Michael Hartl"
        email    "michael@example.com"
        password "foobar"
        password_confirmation "foobar"
      end
    end

Generate Rails Application with RSpec

Step by step guide:

 1) Generate a new application without the default Test:Unit
$ rails new sample_app --skip-test-unit

2) Add the RSpec gem in the Gemfile
 group :development, :test do
   gem 'rspec-rails', '2.11.0'
      gem 'shoulda-matchers'
 end
 
 # Add PostgreSQL for production (optional).
 group :production do
   gem 'pg', '0.12.2'
 end

3) Include the Capybara gem, which allows us to simulate a user's interaction with the sample application using a natural English-like syntax.
 group :test do
   gem 'capybara', '1.1.2'
 end

4) Install the gems with "--without production" option. This is a remembered option, which means that we don't have to include it in future invocations of Bundler. Instead, we can write simply bundle install and production gems will be ignored automatically.
 $ bundle install --without production

5) Configure Rails to use RSpec (Note the single colon ":").
 $ rails generate rspec:install  
Note: Add --format doc in .rspec file to make the test output hierarchical

 6) Generate a model with
 $ rails generate model Person  # This will create the model class, migration and test

7) Fill-up the migration and run it 9) Run the test. If the following error "Cannot find table 'people'", it's because the test database is not ready.
    $ rake db:test:prepare              # This just ensures that the data model from the development database (db/development.sqlite3) is reflected in the test database (db/test.sqlite3)

Recent Post