Cucumber Scaffolding for Rails

December 27th, 2009

I recently discovered the rspec_scaffold generator, part of rspec_on_rails. It generates a resource just as the normal Rails one does, and rspec tests instead of normal tests, as the example below shows:

$ script/generate rspec_scaffold person name:string admin:boolean birthday:date
      exists  app/models/
      exists  app/controllers/
      exists  app/helpers/
      create  app/views/people
      exists  app/views/layouts/
      exists  public/stylesheets/
      exists  spec/controllers/
      exists  spec/routing/
      exists  spec/models/
      exists  spec/helpers/
      exists  spec/fixtures/
      create  spec/views/people
      exists  spec/integration/
      create  app/views/layouts/people.html.erb
   identical  public/stylesheets/scaffold.css
      create  spec/routing/people_routing_spec.rb
      create  spec/controllers/people_controller_spec.rb
      create  app/controllers/people_controller.rb
      create  spec/helpers/people_helper_spec.rb
      create  app/helpers/people_helper.rb
      create  app/views/people/index.html.erb
      create  app/views/people/show.html.erb
      create  app/views/people/new.html.erb
      create  app/views/people/edit.html.erb
      create  app/models/person.rb
      create  spec/fixtures/people.yml
      create  spec/models/person_spec.rb
      create  spec/views/people/edit.html.erb_spec.rb
      create  spec/views/people/index.html.erb_spec.rb
      create  spec/views/people/new.html.erb_spec.rb
      create  spec/views/people/show.html.erb_spec.rb
      create  spec/integration/people_spec.rb
      exists  db/migrate
      create  db/migrate/20091227214143_create_people.rb
       route  map.resources :people

But unlike the normal tests, these aren’t just skeleton files. They include a wide range of tests for the model and it’s controller, views and routes.

The thing that’s missing is an integration test, such as the following Cucumber feature:
Feature: Manage people

  Scenario: List people
    Given a person exists with name "Homer Simpson" 
      And a person exists with name "Bart Simpson" 
    When I go to the people page
    Then the title should be "People: index" 
      And the heading should be "Listing people" 
      And I should see "Homer Simpson" 
      And I should see "Bart Simpson" 

  Scenario: View person
    Given a person exists with name "Homer Simpson" 
    When I go to the people page
      And I follow "Show" 
    Then I should be on the page for that person
      And the title should be "People: show" 
      And I should see "Homer Simpson" against "Name:" 

  Scenario: New person
    Given I am on the people page
    When I follow "New person" 
    Then I should be on the new person page
      And the title should be "People: new" 
      And the heading should be "New person" 
      And the person's birthday should be today
      And the person's birthday year range should be from 5 years ago to 5 years from now

  Scenario: Create person
    Given I am on the new person page
    When I fill in "Name" with "Homer Simpson" 
      And I check "admin" 
      And I select "December 31, 2007" as the "Birthday" date
      And I press "Create" 
    Then I should be on the page for person "Homer Simpson" 
      And I should see "Homer Simpson" against "Name:" 
      And I should see "true" against "Admin:" 
      And I should see "2007-12-31" against "Birthday:" 
      And I should see "Person was successfully created." 

  Scenario: Back from new person page
    Given I am on the new person page
    When I follow "Back" 
    Then I should be on the people page

  Scenario: Back from edit person page
    Given a person exists with name "Homer Simpson" 
    When I go to the edit page for that person
      And I follow "Back" 
    Then I should be on the people page

  Scenario: Show from edit person page
    Given a person exists with name "Homer Simpson" 
    When I go to the edit page for that person
      And I follow "Show" 
    Then I should be on the page for that person

  Scenario: Edit from show person page
    Given a person exists with name "Homer Simpson" 
    When I go to the page for that person
      And I follow "Edit" 
    Then I should be on the edit page for that person

  Scenario: Edit person
    Given a person exists with name "Homer Simpson" 
      And that person is admin
      And that person has "birthday" date "February 14, 2010" 
    When I go to the people page
      And I follow "Edit" 
    Then I should be on the edit page for that person
      And the title should be "People: edit" 
      And the heading should be "Editing person" 
      And the "name" field should contain "Homer Simpson" 
      And the "admin" checkbox should be checked
      And the person's birthday should be "February 14, 2010" 

  Scenario: Update person
    Given a person exists with name "Homer Simpson" 
      And that person is admin
    When I go to the edit page for that person
      And I fill in "name" with "Homer J Simpson" 
      And I uncheck "admin" 
      And I press "Update" 
    Then I should be on the page for person "Homer J Simpson" 
      And I should see "Person was successfully updated." 
      And I should see "Homer J Simpson" against "Name:" 
      And I should see "false" against "Admin:" 

I wrote the above manually but I plan to automate this and turn it into a gem eventually, so that you’ll be able to do something like this:

$ script/generate cucumber_scaffold person name:string admin:boolean birthday:date

Leave a Reply