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.

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

Instance Method Summary collapse

Instance Attribute Details

#descObject (readonly)

:stopdoc:



278
279
280
# File 'lib/minitest/spec.rb', line 278

def desc
  @desc
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.



191
192
193
194
195
196
# File 'lib/minitest/spec.rb', line 191

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

#after_last(type = nil, &block) ⇒ Object



198
199
200
201
202
203
# File 'lib/minitest/spec.rb', line 198

def after_last type = nil, &block
  define_method :after_last_method 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.



177
178
179
180
181
182
# File 'lib/minitest/spec.rb', line 177

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

#before_first(type = nil, &block) ⇒ Object



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

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

#childrenObject

Returns the children of this spec.



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

def children
  @children ||= []
end

#create(name, desc) ⇒ Object

:nodoc:



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

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:



146
147
148
# File 'lib/minitest/spec.rb', line 146

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

#it(desc = "anonymous", &block) ⇒ Object Also known as: t, 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 inheritence, use minitest/test. You can mix and match between assertions and expectations as much as you want.



216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
# File 'lib/minitest/spec.rb', line 216

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

  @specs ||= 0
  @specs += 1

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

  define_method name, &block

  self.children.each do |mod|
    mod.send :undef_method, name if mod.public_method_defined? 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)


240
241
242
243
244
245
246
# File 'lib/minitest/spec.rb', line 240

def let name, &block
  raise ArgumentError, 'name cannot begin with "test"' if name.to_s =~ /\Atest/
  define_method name do
    @_memoized ||= {}
    @_memoized.fetch(name) { |k| @_memoized[k] = instance_eval(&block) }
  end
end

#nameObject

:nodoc:



269
270
271
# File 'lib/minitest/spec.rb', line 269

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

#nuke_test_methods!Object

:nodoc:



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

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


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

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

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

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


136
137
138
139
140
141
142
143
144
# File 'lib/minitest/spec.rb', line 136

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

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



252
253
254
# File 'lib/minitest/spec.rb', line 252

def subject &block
  let :subject, &block
end

#to_sObject

:nodoc:



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

def to_s # :nodoc:
  name # Can't alias due to 1.8.7, not sure why
end