Notes: TD & BDD

Notes taken from the book: Rails 3 in Action

  • In the Ruby world a huge emphasis is placed on testing, specifically on test-driven devel- opment (TDD – Test::Unit) and behavior-driven development (BDD – RSpec, Cucumber etc.).
  • TDD example:
require 'test/unit'
class ExampleTest < Test::Unit::TestCase
  def test_truth
    assert true
  end
end
  • RSpec Example: (to run: rspec spec filename)
           require 'bacon'
           describe Bacon do
              subject { Bacon.new }
              it "is edible" do
                Bacon.new.edible?.should be_true                     
              end
              it "expired!" do
                bacon = Bacon.new
                bacon.expired!
                bacon.expired.should be_true
               end
            end
  • Aslak Hellesoy rewrote RSpec Stories during October 2008 into what we know today as Cucumber.
  • Cucumber Examples: (run: cucumber features )
Feature: My Account
          In order to manage my account
          As a money minder
          I want to ensure my money doesn't get lost

          Scenario: Taking out money
            Given I have an account
            And it has a balance of 100
            When I take out 10

            Then my balance should be 90

Steps Definitions:

        Given /^I have an account$/ do
          pending # express the regexp above with the code you wish you had
        end
        Given /^it has a balance of (\d+)$/ do |arg1|
          pending # express the regexp above with the code you wish you had
        end
        When /^I take out (\d+)$/ do |arg1|
          pending # express the regexp above with the code you wish you had
        end
        Then /^my balance should be (\d+)$/ do |arg1|
          pending # express the regexp above with the code you wish you had
        end