Module: Minitest::Spec::DSL

Included in:
BenchSpec, Minitest::Spec
Defined in:
lib/minitest/spec.rb

Overview

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

Defined Under Namespace

Modules: InstanceMethods

Constant Summary collapse

TYPES =

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

[[//, Minitest::Spec]]

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#descObject (readonly)

:nodoc:



274
275
276
# File 'lib/minitest/spec.rb', line 274

def desc
  @desc
end

Class Method Details

.extended(obj) ⇒ Object

:nodoc:



310
311
312
# File 'lib/minitest/spec.rb', line 310

def self.extended obj # :nodoc:
  obj.send :include, InstanceMethods
end

Instance Method Details

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



189
190
191
192
193
194
# File 'lib/minitest/spec.rb', line 189

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

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



175
176
177
178
179
180
# File 'lib/minitest/spec.rb', line 175

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

#childrenObject

:nodoc:



158
159
160
# File 'lib/minitest/spec.rb', line 158

def children # :nodoc:
  @children ||= []
end

#create(name, desc) ⇒ Object

:nodoc:



254
255
256
257
258
259
260
261
262
263
264
265
# File 'lib/minitest/spec.rb', line 254

def create name, desc # :nodoc:
  cls = Class.new self do
    @name = name
    @desc = desc

    nuke_test_methods!
  end

  children << cls

  cls
end

#describe_stackObject

:nodoc:



154
155
156
# File 'lib/minitest/spec.rb', line 154

def describe_stack # :nodoc:
  Thread.current[:describe_stack] ||= []
end

#it(desc = "anonymous", &block) ⇒ Object 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.



207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
# File 'lib/minitest/spec.rb', line 207

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) ⇒ Object

Essentially, define an accessor for name with block.

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

Raises:

  • (ArgumentError)


231
232
233
234
235
236
237
238
239
240
241
242
243
244
# File 'lib/minitest/spec.rb', line 231

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.start_with? "test"
  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

#nameObject Also known as: to_s, inspect

:nodoc:



267
268
269
# File 'lib/minitest/spec.rb', line 267

def name # :nodoc:
  defined?(@name) ? @name : super
end

#nuke_test_methods!Object

:nodoc:



162
163
164
165
166
# File 'lib/minitest/spec.rb', line 162

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

#register_spec_type(*args, &block) ⇒ Object

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


130
131
132
133
134
135
136
137
# File 'lib/minitest/spec.rb', line 130

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) ⇒ Object

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

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


144
145
146
147
148
149
150
151
152
# File 'lib/minitest/spec.rb', line 144

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) ⇒ Object

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



250
251
252
# File 'lib/minitest/spec.rb', line 250

def subject &block
  let :subject, &block
end