Module: Test::Unit::Given::TestThat::ClassMethods

Defined in:
lib/test/unit/given/test_that.rb

Instance Method Summary collapse

Instance Method Details

#test_that(description = nil, &block) ⇒ Object

Public: Create a new test method with the given optional description and body.

description - a string describing what is being tested; write this as a follow on

from the phrase "test that".  If nil, a name will be constructed
based on the block

block - the body of the test.

Example

# Create a rails-style test method
test_that "area is computed based on positive radius" do
  Given {
    @circle = Circle.new(10)
  }
  When {
    @area = @circle.area
  }
  Then {
    assert_equal 314,@area
  }
end

# Create an "anonymous" test, where the test body
# is clear enough as to what's being tested
test_that {
  Given a_circle(:radius => 10)
  When get_area
  Then area_should_be(314)
}


43
44
45
46
47
48
49
50
# File 'lib/test/unit/given/test_that.rb', line 43

def test_that(description=nil,&block)
  raise "You must provide a block" if block.nil?
  description = make_up_name(block) if description.nil?
  test_name = "test_#{description.gsub(/\s+/,'_')}".to_sym
  defined = instance_method(test_name) rescue false
  raise "#{test_name} is already defined in #{self}" if defined
  define_method(test_name, &block)
end