Module: Minitest::Spec::DSL
Relationships & Source Files | |
Namespace Children | |
Modules:
| |
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
-
TYPES =
Contains pairs of matchers and
::Minitest::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
[[//, Minitest::Spec]]
Class Method Summary
- .extended(obj) Internal use only
Instance Attribute Summary
- #desc readonly Internal use only
Instance Method Summary
-
#after(_type = nil, &block)
Define an ‘after’ action.
-
#before(_type = nil, &block)
Define a ‘before’ action.
-
#it(desc = "anonymous", &block)
(also: #specify)
Define an expectation with name #desc.
-
#let(name, &block)
Essentially, define an accessor for #name with
block
. -
#register_spec_type(*args, &block)
Register a new type of spec that matches the spec’s description.
-
#spec_type(desc, *additional)
Figure out the spec class to use based on a spec’s description.
-
#specify(desc = "anonymous", &block)
Alias for #it.
-
#subject(&block)
Another lazy man’s accessor generator.
- #children Internal use only
- #create(name, desc) Internal use only
- #describe_stack Internal use only
- #name Internal use only
- #nuke_test_methods! Internal use only
- #to_s Internal use only
Class Method Details
.extended(obj)
# File 'lib/minitest/spec.rb', line 335
def self.extended obj # :nodoc: obj.send :include, InstanceMethods end
Instance Attribute Details
#desc (readonly)
# File 'lib/minitest/spec.rb', line 291
attr_reader :desc # :nodoc:
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
.
# File 'lib/minitest/spec.rb', line 205
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
.
# File 'lib/minitest/spec.rb', line 191
def before _type = nil, &block define_method :setup do super() self.instance_eval(&block) end end
#children
# File 'lib/minitest/spec.rb', line 174
def children # :nodoc: @children ||= [] end
#create(name, desc)
#describe_stack
# File 'lib/minitest/spec.rb', line 170
def describe_stack # :nodoc: Thread.current[:describe_stack] ||= [] 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.
# File 'lib/minitest/spec.rb', line 223
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.
# File 'lib/minitest/spec.rb', line 247
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
#name
# File 'lib/minitest/spec.rb', line 283
def name # :nodoc: defined?(@name) ? @name : super end
#nuke_test_methods!
#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 'lib/minitest/spec.rb', line 146
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
#specify(desc = "anonymous", &block)
Alias for #it.
# File 'lib/minitest/spec.rb', line 292
alias :specify :it
#subject(&block)
Another lazy man’s accessor generator. Made even more lazy by setting the name for you to subject
.
# File 'lib/minitest/spec.rb', line 266
def subject &block let :subject, &block end
#to_s
# File 'lib/minitest/spec.rb', line 287
def to_s # :nodoc: name # Can't alias due to 1.8.7, not sure why end