Class: Test::Unit::TestCase

Inherits:
Object
  • Object
show all
Defined in:
lib/test4requirements/shoulda.rb,
lib/test4requirements/testcase.rb

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.get_requirementsObject



23
# File 'lib/test4requirements/testcase.rb', line 23

def get_requirements; @requirements; end

.requirements=(req) ⇒ Object Also known as: requirements

Define requirements, handled by the TestCase-object.

Raises:

  • (ArgumentError)


15
16
17
18
19
# File 'lib/test4requirements/testcase.rb', line 15

def requirements=(req)
  raise ArgumentError, "#{self.class}: Expected RequirementList, received #{req.inspect}" unless req.is_a?(Test4requirements::RequirementList)
  raise ArgumentError, "#{self.class}: Get two RequirementLists" if defined? @requirements
  @requirements = req
end

.should(name_or_matcher, options = {}, &blk) ⇒ Object

Extend #should to support assignment to requirement.

Usage:

should 'fullfill request 1', requirement: :req1  do
  assert_equal(2,1+1)
end


40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
# File 'lib/test4requirements/shoulda.rb', line 40

def should(name_or_matcher, options = {}, &blk)
  if ! self.name
    raise RuntimeError, "Shoulda not defined for anonymous classes"
  end
  if options[:requirement]
    #testmethodname must fit self.name of the later test (TestCase#run_test)
    testmethodname = 'test: '
    testmethodname << self.name.gsub(/Test/, "")
    testmethodname << ' should '
    testmethodname << name_or_matcher
    testmethodname << '. '
    testmethodname << "(#{self.name})"
    raise RuntimeError, "No requirements defined for #{self}" unless get_requirements
    get_requirements.assign_test(options[:requirement], testmethodname, Test4requirements::TestResults.new)
  end
  should_old(name_or_matcher, options = {}, &blk)
end

.should_oldObject

remember original definition.



31
# File 'lib/test4requirements/shoulda.rb', line 31

alias  :should_old :should

Instance Method Details

#assign_requirement(req) ⇒ Object

Assign a requirement. To be called inside a test_*-method.

Example:

def test_1()
  assign_requirement(:req1)  #this test is testing requirement 1
  assert_equal(2,1+1)
end

Raises:

  • (RuntimeError)


41
42
43
44
# File 'lib/test4requirements/testcase.rb', line 41

def assign_requirement(req)
  raise RuntimeError, "No requirements defined for #{self}" unless requirements
  requirements.assign_test(req, self.name, @_result)
end

#old_run_testObject



46
# File 'lib/test4requirements/testcase.rb', line 46

alias :old_run_test :run_test

#requirementsObject

Get Requirements for TestCases



28
29
30
# File 'lib/test4requirements/testcase.rb', line 28

def requirements
  self.class.get_requirements
end

#run_testObject



47
48
49
50
51
52
53
# File 'lib/test4requirements/testcase.rb', line 47

def run_test
  old_run_test
  #Store successfull test run,
  self.requirements.each{|req|
    req.result(self.name, :ok) unless req[self.name].nil?
  } if requirements
end