Saturday, October 12, 2013

Test Driven Development for Ruby: Test:Unit, Minitest and Rspec



There are several different frameworks that can be used to implement test driven development in Ruby. These include Rspec, Test:Unit and Minitest. The Test:Unit comes built into Ruby. This means that you won't have to install the associated gem.

The following source code  file, persontest.rb,  from http://lakedenman.com/2011/01/08/exploring-ruby-test-unit-assert-method.html,  works as given. The require method  imports the Test:Unit framework into the Ruby program.

Before you decide to cut and paste this code to run, consider obtaining a Ruby text editor to paste it into. If you are using notepad or other simple text programs as your editor, don't. The line breaks can be critical. Instead use an all-purpose code editor such as Sublime Text. This Ruby text editor can be downloaded at  http://www.sublimetext.com/

#Name of File: persontest.rb

require 'test/unit'

class Person
  attr_reader :age
  def initialize
    @age = 0
  end
end

class TestPerson < Test::Unit::TestCase
  def test_new_person_age_is_zero
    assert(Person.new.age.zero?)
  end
end

Source: Lakedman.com. http://lakedenman.com/2011/01/08/exploring-ruby-test-unit-assert-method.html




The first part of the video sets up the project directory for a test driven development in Ruby:  http://rspec.codeschool.com/levels/1. 

Test Driven Development(TDD) Introductory Course Video Series

The Ruby Installer: Download Ruby in One Click
The Rspec Book, Free PDF Book Download

Ruby Programming A-Z: Includes working code examples by introductory topic

RubyMonk Learning Books: A complete set of books on Ruby online that let you run the code online. Complete explanations for each online piece of code.

Code with Online Compiler: Gives simplified explanations

Ruby Manual Code School:  A complete book on Ruby fundamentals

Use of Describe and It functions for Test Driven Developmen:



Ruby in Twenty Minutes: Emphasizes Command Line Prompt Mode

Obtaining a Directory Listing with Ruby in irb Command Line Prompt Mode

http://en.wikibooks.org/wiki/Ruby_Programming/Unit_testing


No comments:

Post a Comment