MiniTest::Spec – The faster, better, less-magical spec framework!

For a list of expectations, see MiniTest::Expectations.

Methods
A
B
C
I
L
R
S
Constants
TYPES = [[//, MiniTest::Spec]]
 

Contains pairs of matchers and Spec classes to be used to calculate the superclass of a top-level describe. This allows for automatically customizable spec types.

See: ::register_spec_type and ::spec_type

Class Public methods
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.

# File ../ruby/lib/minitest/spec.rb, line 183
def self.after type = :each, &block
  raise "unsupported after type: #{type}" unless type == :each

  add_teardown_hook {|tc| tc.instance_eval(&block) }
end
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.

# File ../ruby/lib/minitest/spec.rb, line 170
def self.before type = :each, &block
  raise "unsupported before type: #{type}" unless type == :each

  add_setup_hook {|tc| tc.instance_eval(&block) }
end
bench(name, &block)

This is used to define a new benchmark method. You usually don't use this directly and is intended for those needing to write new performance curve fits (eg: you need a specific polynomial fit).

See ::bench_performance_linear for an example of how to use this.

# File ../ruby/lib/minitest/benchmark.rb, line 317
def self.bench name, &block
  define_method "bench_#{name.gsub(/\W+/, '_')}", &block
end
bench_performance_constant(name, threshold = 0.99, &work)

Create a benchmark that verifies that the performance is constant.

describe "my class" do
  bench_performance_constant "zoom_algorithm!" do
    @obj.zoom_algorithm!
  end
end
# File ../ruby/lib/minitest/benchmark.rb, line 352
def self.bench_performance_constant name, threshold = 0.99, &work
  bench name do
    assert_performance_constant threshold, &work
  end
end
bench_performance_exponential(name, threshold = 0.99, &work)

Create a benchmark that verifies that the performance is exponential.

describe "my class" do
  bench_performance_exponential "algorithm" do
    @obj.algorithm
  end
end
# File ../ruby/lib/minitest/benchmark.rb, line 367
def self.bench_performance_exponential name, threshold = 0.99, &work
  bench name do
    assert_performance_exponential threshold, &work
  end
end
bench_performance_linear(name, threshold = 0.99, &work)

Create a benchmark that verifies that the performance is linear.

describe "my class" do
  bench_performance_linear "fast_algorithm", 0.9999 do
    @obj.fast_algorithm
  end
end
# File ../ruby/lib/minitest/benchmark.rb, line 337
def self.bench_performance_linear name, threshold = 0.99, &work
  bench name do
    assert_performance_linear threshold, &work
  end
end
bench_range(&block)
# File ../ruby/lib/minitest/benchmark.rb, line 321
def self.bench_range &block
  return super unless block

  meta = (class << self; self; end)
  meta.send :define_method, "bench_range", &block
end
children()

Returns the children of this spec.

# File ../ruby/lib/minitest/spec.rb, line 148
def self.children
  @children ||= []
end
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.

# File ../ruby/lib/minitest/spec.rb, line 198
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

  self.children.each do |mod|
    mod.send :undef_method, name if mod.public_method_defined? name
  end
end
let(name, &block)
# File ../ruby/lib/minitest/spec.rb, line 213
def self.let name, &block
  define_method name do
    @_memoized ||= {}
    @_memoized.fetch(name) { |k| @_memoized[k] = instance_eval(&block) }
  end
end
register_spec_type(*args, &block)

Register a new type of spec that matches the spec's description. This method can take either a Regexp and a spec class or a spec class and a block that takes the description and returns true if it matches.

Eg:

register_spec_type(/Controller$/, MiniTest::Spec::Rails)

or:

register_spec_type(MiniTest::Spec::RailsModel) do |desc|
  desc.superclass == ActiveRecord::Base
end
# File ../ruby/lib/minitest/spec.rb, line 112
def self.register_spec_type(*args, &block)
  if block then
    matcher, klass = block, args.first
  else
    matcher, klass = *args
  end
  TYPES.unshift [matcher, klass]
end
spec_type(desc)

Figure out the spec class to use based on a spec's description. Eg:

spec_type("BlahController") # => MiniTest::Spec::Rails
# File ../ruby/lib/minitest/spec.rb, line 126
def self.spec_type desc
  TYPES.find { |matcher, klass|
    if matcher.respond_to? :call then
      matcher.call desc
    else
      matcher === desc.to_s
    end
  }.last
end
subject(&block)
# File ../ruby/lib/minitest/spec.rb, line 220
def self.subject &block
  let :subject, &block
end