Class: MiniTest::Spec

Inherits:
Unit::TestCase show all
Defined in:
lib/minitest/spec.rb

Constant Summary collapse

@@describe_stack =
[MiniTest::Spec]

Constants inherited from Unit::TestCase

Unit::TestCase::PASSTHROUGH_EXCEPTIONS, Unit::TestCase::SUPPORTS_INFO_SIGNAL

Instance Attribute Summary

Attributes inherited from Unit::TestCase

#__name__

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Unit::TestCase

inherited, #passed?, reset, #run, #setup, #teardown, test_methods, test_order, test_suites

Methods included from Assertions

#_assertions, #_assertions=, #assert, #assert_block, #assert_empty, #assert_equal, #assert_in_delta, #assert_in_epsilon, #assert_includes, #assert_instance_of, #assert_kind_of, #assert_match, #assert_nil, #assert_operator, #assert_output, #assert_raises, #assert_respond_to, #assert_same, #assert_send, #assert_silent, #assert_throws, #capture_io, #exception_details, #flunk, #message, #mu_pp, #pass, #refute, #refute_empty, #refute_equal, #refute_in_delta, #refute_in_epsilon, #refute_includes, #refute_instance_of, #refute_kind_of, #refute_match, #refute_nil, #refute_operator, #refute_respond_to, #refute_same, #skip

Constructor Details

#initialize(name) ⇒ Spec

:nodoc:



102
103
104
105
# File 'lib/minitest/spec.rb', line 102

def initialize name # :nodoc:
  super
  @@current_spec = self
end

Class Method Details

.after(type = :each, &block) ⇒ Object

Define an ‘after’ action. Inherits the way normal methods should.

NOTE: type is ignored and is only there to make porting easier.

Equivalent to MiniTest::Unit::TestCase#teardown.



141
142
143
144
# File 'lib/minitest/spec.rb', line 141

def self.after type = :each, &block
  raise "unsupported after type: #{type}" unless type == :each
  define_inheritable_method :teardown, &block
end

.before(type = :each, &block) ⇒ Object

Define a ‘before’ action. Inherits the way normal methods should.

NOTE: type is ignored and is only there to make porting easier.

Equivalent to MiniTest::Unit::TestCase#setup.



129
130
131
132
# File 'lib/minitest/spec.rb', line 129

def self.before type = :each, &block
  raise "unsupported before type: #{type}" unless type == :each
  define_inheritable_method :setup, &block
end

.currentObject

:nodoc:



98
99
100
# File 'lib/minitest/spec.rb', line 98

def self.current # :nodoc:
  @@current_spec
end

.define_inheritable_method(name, &block) ⇒ Object

:nodoc:



113
114
115
116
117
118
119
120
# File 'lib/minitest/spec.rb', line 113

def self.define_inheritable_method name, &block # :nodoc:
  super_method = self.superclass.instance_method name

  define_method name do
    super_method.bind(self).call if super_method # regular super() warns
    instance_eval(&block)
  end
end

.describe_stackObject

:nodoc:



94
95
96
# File 'lib/minitest/spec.rb', line 94

def self.describe_stack # :nodoc:
  @@describe_stack
end

.it(desc, &block) ⇒ Object

Define an expectation with name desc. Name gets morphed to a proper test method name. For some freakish reason, people who write specs don’t like class inheritence, so this goes way out of its way to make sure that expectations aren’t inherited.

Hint: If you do want inheritence, use minitest/unit. You can mix and match between assertions and expectations as much as you want.



155
156
157
158
159
160
161
162
163
164
165
166
167
168
# File 'lib/minitest/spec.rb', line 155

def self.it desc, &block
  block ||= proc { skip "(no tests defined)" }

  @specs ||= 0
  @specs += 1

  name = "test_%04d_%s" % [ @specs, desc.gsub(/\W+/, '_').downcase ]

  define_method name, &block

  classes(MiniTest::Spec).each do |mod|
    mod.send :undef_method, name if mod.respond_to? name
  end
end

.nuke_test_methods!Object

:nodoc:



107
108
109
110
111
# File 'lib/minitest/spec.rb', line 107

def self.nuke_test_methods! # :nodoc:
  self.public_instance_methods.grep(/^test_/).each do |name|
    self.send :undef_method, name
  end
end