Method: Context::TestCase::ClassMethods#context

Defined in:
lib/context/core.rb

#context(name, &block) ⇒ Object

Add a context to a set of tests.

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

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

1) Failure:
test_a_new_account_should_not_have_users() [./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.



75
76
77
78
79
80
81
82
83
84
85
86
87
88
# File 'lib/context/core.rb', line 75

def context(name, &block)
  cls = Class.new(self)
  cls.context_name = name

  # Care about Rails tests in nested contexts
  cls.tests($1.constantize) if defined?(Rails) && 
    self.name =~ /^(.*(Controller|Helper|Mailer))Test/ && 
      self < ActiveSupport::TestCase

  cls.class_eval(&block)
  (self.context_list ||= []) << cls
  const_set("Test#{name.to_class_name}#{cls.object_id.abs}", cls)
  cls
end