16 March 2012

Spent the whole day learning Test Driven Development (TDD) with Dennis as my partner in a paired programmer setup. Heya Dennis! The vehicle we chose for learning was Cucumber. I have to admit I am loving Cucumber! Cucumber is setup so that you describe the story/requirement up front followed by what is essentially a use case that is backed up by code that actually executes. Now today I used it to perform testing on a class. Normally that would be handled by RSpec and Cucumber would be used for testing at the applicaton level. This according to our trainers Guy Royse and George Walters. Anything that I attribute to them that isn’t correct is probably my fault:)

Now we did several scenarios during the TDD session with Guy and Walter, but I though I would just show you one of my favorites. This demonstrates feeding data into template and then checking to verify the methods exist dynamically.

evercraft/features/evercraft_create_character.feature
Feature: evercraft creates character

  As a player
  I want to create a character
  So that I can customize it

  Scenario Outline:  character has attributes
    Given I have a character
    Then I see the character has <attribute>
    Examples:
      |attribute   |
      |strength    |
      |intelligence|
      |dexterity   |
      |constitution|
      |charisma    |
      |wisdom      |
evercraft/features/step_definitions/evercraft_steps.rb
  Given /^I have a character$/ do
    @character = EverCraft::Character.new
  end

  Then /^I see the character has (w*)$/ do |attribute|
    @character.respond_to?(attribute).should == true
  end
evercraft/lib/EverCraft/character.rb
module EverCraft
  class Character
    attr_accessor :name, :alignment, :armor_class, :hit_points, :strength, :dexterity, :constitution, :intelligence, :wisdom, :charisma

    def initialize
      self.armor_class=10
      self.hit_points=5
      self.charisma=10
      self.constitution=10
      self.wisdom=10
      self.dexterity=10
      self.strength=10
      self.intelligence=10
    end
  end
end
Results of running "cucumber features" in the evercraft directory
Feature: evercraft creates character
  
  As a player
  I want to create a character
  So that I can customize it

  Scenario Outline: character has attributes # features/evercraft_create_character.feature:7
    <span style="color: Cyan">Given I have a character</span>                 # features/step_definitions/evercraft_steps.rb:1
    <span style="color: Cyan">Then I see the character has &#060;attribute&#062;</span> # features/step_definitions/evercraft_steps.rb:5

    Examples: 
      | <span style="color: Cyan;font-weight: Bold;">attribute</span>    |
      | <span style="color: Green;">strength</span>     |
      | <span style="color: Green;">intelligence</span> |
      | <span style="color: Green;">dexterity</span>    |
      | <span style="color: Green;">constitution</span> |
      | <span style="color: Green;">charisma</span>     |
      | <span style="color: Green;">wisdom</span>       |

6 scenarios (<span style="color: Green;">6 passed</span>)
12 steps (<span style="color: Green;">12 passed</span>)
0m0.014s

Less Is More ~ Older posts are available in the archive.