Red, Green, Refactor! A Workflow for Rails

July 03, 2013

1. Write a failing test

describe "Static About page" do
  it "should have the content 'About Us'" do
    visit '/static_pages/about'
    page.should have_content('About Us')
  end
end

Use the failures to drive development:

2. No Route

Failures:
  1) Static About page should have the content 'About Us'
     Failure/Error: visit '/static_pages/about'
     ActionController::RoutingError:
       No route matches [GET] "/static_pages/about"

Add one to /config/routes.rb:

DemoApp2::Application.routes.draw do
  get "static_pages/about"
  ...
end

3.No Controller

Failures:

  1) Static About page should have the content 'About Us'
     Failure/Error: visit '/static_pages/about'
     AbstractController::ActionNotFound:
       The action 'about' could not be found for StaticPagesController

Add one to /controllers/staticpagescontroller.rb

class StaticPagesController < ApplicationController
  def about
  end
end

4. No Page

Failures:
  1) Static About page should have the content 'About Us'
     Failure/Error: visit '/static_pages/about'
     ActionView::MissingTemplate:
       Missing template static_pages/about, application/about with {:locale=>[:en], :formats=>[:html], :handlers=>[:erb, :builder, :raw, :ruby, :jbuilder, :coffee]}.

Add a view to /static_pages/about.html.erb

<!DOCTYPE html>
  <body>
    <h1>About Us</h1>
  </body>
</html>

Katie Leonard

Mostly Katie explaining things to herself.

© 2025