Module: Thoughtbot::Shoulda

Included in:
Test::Unit::TestCase
Defined in:
lib/shoulda/gem/shoulda.rb

Defined Under Namespace

Classes: Context

Constant Summary collapse

VERSION =
'1.1.1'

Class Attribute Summary collapse

Instance Method Summary collapse

Class Attribute Details

.current_contextObject

Returns the value of attribute current_context.



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

def current_context
  @current_context
end

Instance Method Details

#context(name, &blk) ⇒ Object

Contexts

A context block groups should statements under a common set of setup/teardown methods.

Context blocks can be arbitrarily nested, and can do wonders for improving the maintainability and readability of your test code.

A context block can contain setup, should, should_eventually, and teardown blocks.

class UserTest << Test::Unit::TestCase
  context "A User instance" do
    setup do
      @user = User.find(:first)
    end

    should "return its full name"
      assert_equal 'John Doe', @user.full_name
    end
  end
end

This code will produce the method "test: A User instance should return its full name. ".

Contexts may be nested. Nested contexts run their setup blocks from out to in before each should statement. They then run their teardown blocks from in to out after each should statement.

class UserTest << Test::Unit::TestCase
  context "A User instance" do
    setup do
      @user = User.find(:first)
    end

    should "return its full name"
      assert_equal 'John Doe', @user.full_name
    end

    context "with a profile" do
      setup do
        @user.profile = Profile.find(:first)
      end

      should "return true when sent :has_profile?"
        assert @user.has_profile?
      end
    end
  end
end

This code will produce the following methods

  • "test: A User instance should return its full name. "

  • "test: A User instance with a profile should return true when sent :has_profile?. "

Just like should statements, a context block can exist next to normal def test_the_old_way; end tests. This means you do not have to fully commit to the context/should syntax in a test file.



111
112
113
114
115
116
117
118
# File 'lib/shoulda/gem/shoulda.rb', line 111

def context(name, &blk)
  if Shoulda.current_context
    Shoulda.current_context.context(name, &blk)
  else
    context = Thoughtbot::Shoulda::Context.new(name, self, &blk)
    context.build
  end
end

#should(name, &blk) ⇒ Object

Should statements

Should statements are just syntactic sugar over normal Test::Unit test methods. A should block contains all the normal code and assertions you’re used to seeing, with the added benefit that they can be wrapped inside context blocks (see below).

Example:

class UserTest << Test::Unit::TestCase

  def setup
    @user = User.new("John", "Doe")
  end

  should "return its full name"
    assert_equal 'John Doe', @user.full_name
  end

end

…will produce the following test:

  • "test: User should return its full name. "

Note: The part before should in the test name is gleamed from the name of the Test::Unit class.



36
37
38
39
40
41
42
43
44
45
46
# File 'lib/shoulda/gem/shoulda.rb', line 36

def should(name, &blk)
  if Shoulda.current_context
    block_given? ? Shoulda.current_context.should(name, &blk) : Should.current_context.should_eventually(name)
  else
    context_name = self.name.gsub(/Test/, "")
    context = Thoughtbot::Shoulda::Context.new(context_name, self) do
      block_given? ? should(name, &blk) : should_eventually(name)
    end
    context.build
  end
end

#should_eventually(name, &blk) ⇒ Object

Just like should, but never runs, and instead prints an ‘X’ in the Test::Unit output.



49
50
51
52
53
54
55
# File 'lib/shoulda/gem/shoulda.rb', line 49

def should_eventually(name, &blk)
  context_name = self.name.gsub(/Test/, "")
  context = Thoughtbot::Shoulda::Context.new(context_name, self) do
    should_eventually(name, &blk)
  end
  context.build
end