Module: Minitest::Sugar

Defined in:
lib/minitest/sugar.rb,
lib/minitest/sugar/version.rb

Constant Summary collapse

VERSION =
"2.1.2"

Instance Method Summary collapse

Instance Method Details

#setup(&block) ⇒ Object

Public: Helper to define a setup method.

Examples

require "minitest/autorun"
require "minitest/sugar"

class TruthTest < Minitest::Test
setup do
  @truth = true
end

test "assert the truth" do
  assert @truth
end
end


45
46
47
48
49
50
51
# File 'lib/minitest/sugar.rb', line 45

def setup(&block)
  define_method(:setup) do
    super()

    instance_exec(&block)
  end
end

#teardown(&block) ⇒ Object

Public: Helper to define a teardown method.

Examples

require "minitest/autorun"
require "minitest/sugar"

class TruthTest < Minitest::Test
setup do
  @truth = true
end

teardown do
  @truth = nil
end

test "assert the truth" do
  assert @truth
end
end


74
75
76
77
78
79
80
# File 'lib/minitest/sugar.rb', line 74

def teardown(&block)
  define_method(:teardown) do
    instance_exec(&block)

    super()
  end
end

#test(name, &block) ⇒ Object

Public: Helper to define a test method using a String.

Examples

require "minitest/autorun"
require "minitest/sugar"

class TruthTest < Minitest::Test
test "assert the truth" do
  assert true
end
end


18
19
20
21
22
23
24
25
26
# File 'lib/minitest/sugar.rb', line 18

def test(name, &block)
  test_name = sprintf("test_%s", name.gsub(/\s+/, "_"))

  if method_defined?(test_name)
    raise "#{ test_name } is already defined in #{ self }"
  end

  define_method(test_name, &block)
end