Module: Context::Context

Included in:
Test::Unit::TestCase
Defined in:
lib/context/context.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#context_listObject

Test::Unit uses ObjectSpace to figure out what Test::Unit:TestCase instances are running Contexts are not named and therefore sometimes get garbage collected. Think of #context_list as the shelter for nameless contexts



6
7
8
# File 'lib/context/context.rb', line 6

def context_list
  @context_list
end

Instance Method Details

#context(name, &block) ⇒ Object

Add a context to a set of tests.

context "A new account" do
  it "should not have users"
    assert .new.users.empty?
  end
end

The context name is prepended to the test name, so failures look like this:

1) Failure:
() [./test/test_accounts.rb:4]:
<false> is not true.

Contexts can also be nested like so:

context "A new account" do
  context "created by the web application" do
    it "should have web as its vendor" do
      assert_equal "web", users(:web_user).vendor
    end
  end
end

Since contexts create a singleton instance of a class, each one must have its own before/after blocks. This will be tweaked in future releases to allow you to chain these blocks from its parent contexts.



46
47
48
49
50
51
52
53
54
55
# File 'lib/context/context.rb', line 46

def context(name, &block)
  cls = Class.new(self)
  cls.context_name = name
  # puts "Creating context #{cls.context_name}"
  cls.class_eval(&block)
  (self.context_list ||= []) << cls
  
  const_set("Test#{name.to_class_name}#{cls.object_id.abs}", cls)
  cls
end

#context_nameObject

:nodoc:



8
9
10
11
12
13
# File 'lib/context/context.rb', line 8

def context_name #:nodoc:
  @context_name ||= ""
  if superclass.respond_to?(:context_name)
    return "#{superclass.context_name} #{@context_name}".gsub(/^\s+/, "")
  end
end

#context_name=(val) ⇒ Object

:nodoc:



15
16
17
# File 'lib/context/context.rb', line 15

def context_name=(val) #:nodoc:
  @context_name = val
end