123456789_123456789_123456789_123456789_123456789_

Module: Minitest::Spec::DSL

Relationships & Source Files
Extension / Inclusion / Inheritance Descendants
Extended In:
Defined in: lib/minitest/spec.rb

Overview

Oh look! A DSL module! Eat your heart out DHH.

Constant Summary

Instance Method Summary

Instance Method Details

#after(type = nil, &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::Test#teardown.

[ GitHub ]

  
# File 'lib/minitest/spec.rb', line 183

def after type = nil, &block
  define_method :teardown do
    self.instance_eval(&block)
    super()
  end
end

#before(type = nil, &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::Test#setup.

[ GitHub ]

  
# File 'lib/minitest/spec.rb', line 169

def before type = nil, &block
  define_method :setup do
    super()
    self.instance_eval(&block)
  end
end

#children

Returns the children of this spec.

[ GitHub ]

  
# File 'lib/minitest/spec.rb', line 152

def children
  @children ||= []
end

#it(desc = "anonymous", &block) Also known as: #specify

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 inheritance, so this goes way out of its way to make sure that expectations aren't inherited.

This is also aliased to #specify and doesn't require a #desc arg.

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

[ GitHub ]

  
# File 'lib/minitest/spec.rb', line 201

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

  @specs ||= 0
  @specs += 1

  name = "test_%04d_%s" % [ @specs, desc ]

  undef_klasses = self.children.reject { |c| c.public_method_defined? name }

  define_method name, &block

  undef_klasses.each do |undef_klass|
    undef_klass.send :undef_method, name
  end

  name
end

#let(name, &block)

Essentially, define an accessor for #name with block.

Why use let instead of def? I honestly don't know.

Raises:

  • (ArgumentError)
[ GitHub ]

  
# File 'lib/minitest/spec.rb', line 225

def let name, &block
  name = name.to_s
  pre, post = "let '#{name}' cannot ", ". Please use another name."
  methods = Minitest::Spec.instance_methods.map(&:to_s) - %w[subject]
  raise ArgumentError, "#{pre}begin with 'test'#{post}" if
    name =~ /\Atest/
  raise ArgumentError, "#{pre}override a method in Minitest::Spec#{post}" if
    methods.include? name

  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
[ GitHub ]

  
# File 'lib/minitest/spec.rb', line 121

def 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, *additional)

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

spec_type("BlahController") # => Minitest::Spec::Rails
[ GitHub ]

  
# File 'lib/minitest/spec.rb', line 135

def spec_type desc, *additional
  TYPES.find { |matcher, klass|
    if matcher.respond_to? :call then
      matcher.call desc, *additional
    else
      matcher === desc.to_s
    end
  }.last
end

#subject(&block)

Another lazy man's accessor generator. Made even more lazy by setting the name for you to subject.

[ GitHub ]

  
# File 'lib/minitest/spec.rb', line 244

def subject &block
  let :subject, &block
end