RUBY TESTING
 Cucumber and RSpec
WHO IS THIS GUY?
   James Thompson
    @plainprogrammer
 james@plainprograms.com

Web Administrator & Student
  @ New Orleans Baptist
   Theological Seminary
WHY DO WE TEST?
WHAT DO WE TEST?
WHAT DON’T WE TEST?
HOW DO WE TEST?
WHAT DO WE TEST WITH?
WHAT IS CUCUMBER
•Behavior driven development tool for Ruby
•Focuses on higher level implementations such as
acceptance tests

•Focuses on story-style, plain English test descriptions
•Follows the GWT (Given, When, Then) pattern for
features
WHAT IS RSPEC

•Behavior driven development tool for Ruby
•Provides a DSL for talking about what code should do
A POSSIBLE PROCESS
•Start with a feature
•Define the steps for your feature
•Write lower-level specs
•Write code to pass specs
•Rinse and Repeat until feature passes
WRITE A FEATURE
# features/calculator.feature
Feature: Addition
  In Order to avoid silly mistakes
  As a math idiot
  I want to be told the sum of two numbers

Scenario: Add two numbers
  Given I have entered 50 into the calculator
  And I have entered 70 into the calculator
  When I press add
  Then the result should be 120 on the screen
WRITE STEPS

# features/step_definitions/calculator_steps.rb
Given /I have entered (.*) into the calculator/ do |n|
  calculator = Calculator.new
  calculator.push(n.to_i)
end
WRITE SPECS
# spec/calculator.spec
describe Calculator do
  before(:each) do
    @calculator = Calculator.new
  end

  it "should respond to push()" do
    @calculator.should respond_to(:push)
  end

  it "should respond to add()" do
    @calculator.should respond_to(:add)
  end
end
CODE UNTIL YOU PASS
class Calculator
  def push(n)
    @args ||= []
    @args << n
  end

 def add
   result = 0

   @args.each do |n|
     result += n
   end

    result
  end
end
RINSE AND REPEAT
QUESTIONS?
MORE RESOURCES


     cukes.info
     rspec.info

Ruby Testing: Cucumber and RSpec

  • 1.
  • 2.
    WHO IS THISGUY? James Thompson @plainprogrammer james@plainprograms.com Web Administrator & Student @ New Orleans Baptist Theological Seminary
  • 3.
    WHY DO WETEST?
  • 4.
  • 5.
  • 6.
    HOW DO WETEST?
  • 7.
    WHAT DO WETEST WITH?
  • 8.
    WHAT IS CUCUMBER •Behaviordriven development tool for Ruby •Focuses on higher level implementations such as acceptance tests •Focuses on story-style, plain English test descriptions •Follows the GWT (Given, When, Then) pattern for features
  • 9.
    WHAT IS RSPEC •Behaviordriven development tool for Ruby •Provides a DSL for talking about what code should do
  • 10.
    A POSSIBLE PROCESS •Startwith a feature •Define the steps for your feature •Write lower-level specs •Write code to pass specs •Rinse and Repeat until feature passes
  • 11.
    WRITE A FEATURE #features/calculator.feature Feature: Addition In Order to avoid silly mistakes As a math idiot I want to be told the sum of two numbers Scenario: Add two numbers Given I have entered 50 into the calculator And I have entered 70 into the calculator When I press add Then the result should be 120 on the screen
  • 12.
    WRITE STEPS # features/step_definitions/calculator_steps.rb Given/I have entered (.*) into the calculator/ do |n| calculator = Calculator.new calculator.push(n.to_i) end
  • 13.
    WRITE SPECS # spec/calculator.spec describeCalculator do before(:each) do @calculator = Calculator.new end it "should respond to push()" do @calculator.should respond_to(:push) end it "should respond to add()" do @calculator.should respond_to(:add) end end
  • 14.
    CODE UNTIL YOUPASS class Calculator def push(n) @args ||= [] @args << n end def add result = 0 @args.each do |n| result += n end result end end
  • 15.
  • 16.
  • 17.
    MORE RESOURCES cukes.info rspec.info