Class: MiniTest::Spec

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

Constant Summary

@@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_raises, #assert_respond_to, #assert_same, #assert_send, #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

- (Spec) initialize(name)

:nodoc:



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

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

Class Method Details

+ (Object) after(type = :each, &block)

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.



148
149
150
151
# File 'lib/minitest/spec.rb', line 148

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

+ (Object) before(type = :each, &block)

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.



136
137
138
139
# File 'lib/minitest/spec.rb', line 136

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

+ (Object) current

:nodoc:



105
106
107
# File 'lib/minitest/spec.rb', line 105

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

+ (Object) define_inheritable_method(name, &block)

:nodoc:



120
121
122
123
124
125
126
127
# File 'lib/minitest/spec.rb', line 120

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

+ (Object) describe_stack

:nodoc:



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

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

+ (Object) it(desc, &block)

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.



162
163
164
165
166
167
168
169
170
171
172
173
174
175
# File 'lib/minitest/spec.rb', line 162

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

+ (Object) nuke_test_methods!

:nodoc:



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

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