Module: Test::Unit::Context::Spec

Defined in:
lib/test/unit/context/spec.rb

Defined Under Namespace

Modules: Methods

Constant Summary collapse

PREFIX =

:nodoc:

nil
SUFFIX =

:nodoc:

'Test'.freeze

Instance Method Summary collapse

Instance Method Details

#describe(*args, &block) ⇒ Object

Spec style Test::Unit::TestCase (sub-classes) :

describe User do
  it "creates a new instance" do
    assert_nothing_raised { User.new }
  end
end

You need to ‘require ’test/unit/context/spec’‘ ( e.g. in test_helper.rb).

The generated Test::Unit::TestCase sub-class follows the “Test” suffix naming convention e.g. ‘describe User` ends up as `UserTest < TestCase`.



20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
# File 'lib/test/unit/context/spec.rb', line 20

def describe(*args, &block) 
  name = args.map { |arg| arg.is_a?(Class) ? arg.name : arg }.join
  class_name = Helpers.to_const_name(name.freeze, PREFIX, SUFFIX)
  self_klass = self.is_a?(Class) ? self : self.class # nested describes
  if self_klass.const_defined?(class_name)
    klass = self_klass.const_get(class_name)
    if klass <= Test::Unit::TestCase
      warn "duplicate test-case describe with args #{args.inspect} " << 
            "found at #{caller.first} it is going to be merged with " << 
            "the previous TestCase definition"
    else
      raise "could not create a test-case with args #{args.inspect} " << 
            "as a constant #{class_name} is already defined and is not " << 
            "another TestCase definition"
    end
  else
    # NOTE: context inherits from nesting case but describe should not :
    #is_test_case = ( self_klass <= Test::Unit::TestCase )
    #klass = Class.new is_test_case ? self_klass : Test::Unit::TestCase
    klass = Class.new(Test::Unit::TestCase)
    klass.send(:define_method, :name) { name }
    klass.extend Methods
    self_klass.const_set(class_name, klass)
  end
  klass.class_eval(&block)
  klass
end