Class: MiniTest::Spec

Inherits:
Unit::TestCase show all
Defined in:
lib/minitest/spec.rb,
lib/minitest/benchmark.rb

Overview

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

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

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]]
@@describe_stack =
[]

Constants inherited from Unit::TestCase

Unit::TestCase::PASSTHROUGH_EXCEPTIONS, Unit::TestCase::SUPPORTS_INFO_SIGNAL

Constants included from Assertions

Assertions::UNDEFINED

Class Attribute Summary collapse

Attributes inherited from Unit::TestCase

#__name__

Class Method Summary collapse

Methods inherited from Unit::TestCase

#assert_performance, #assert_performance_constant, #assert_performance_exponential, #assert_performance_linear, #assert_performance_power, bench_exp, bench_linear, benchmark_methods, benchmark_suites, current, #fit_error, #fit_exponential, #fit_linear, #fit_power, i_suck_and_my_tests_are_order_dependent!, inherited, #initialize, #io, #io?, make_my_diffs_pretty!, parallelize_me!, #passed?, reset, reset_setup_teardown_hooks, #run, #setup, #sigma, #teardown, test_methods, test_order, test_suites, #validation_for_fit

Methods included from Unit::Guard

#jruby?, #mri?, #rubinius?, #windows?

Methods included from Unit::Deprecated::HooksCM

#add_setup_hook, #add_teardown_hook, #setup_hooks, #teardown_hooks

Methods included from Assertions

#_assertions, #_assertions=, #assert, #assert_block, #assert_empty, #assert_equal, #assert_in_delta, #assert_in_epsilon, #assert_includes, #assert_instance_of, #assert_kind_of, #assert_match, #assert_nil, #assert_operator, #assert_output, #assert_predicate, #assert_raises, #assert_respond_to, #assert_same, #assert_send, #assert_silent, #assert_throws, #capture_io, #capture_subprocess_io, diff, #diff, diff=, #exception_details, #flunk, #message, #mu_pp, #mu_pp_for_diff, #pass, #refute, #refute_empty, #refute_equal, #refute_in_delta, #refute_in_epsilon, #refute_includes, #refute_instance_of, #refute_kind_of, #refute_match, #refute_nil, #refute_operator, #refute_predicate, #refute_respond_to, #refute_same, #skip, #synchronize

Methods included from Unit::Deprecated::Hooks

#_run_hooks, #run_setup_hooks, #run_teardown_hooks

Methods included from Unit::LifecycleHooks

#after_setup, #after_teardown, #before_setup, #before_teardown

Constructor Details

This class inherits a constructor from MiniTest::Unit::TestCase

Class Attribute Details

.descObject (readonly)

Returns the value of attribute desc


246
247
248
# File 'lib/minitest/spec.rb', line 246

def desc
  @desc
end

Class 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::Unit::TestCase#teardown.


172
173
174
175
176
177
# File 'lib/minitest/spec.rb', line 172

def self.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::Unit::TestCase#setup.


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

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

.bench(name, &block) ⇒ Object

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.


309
310
311
# File 'lib/minitest/benchmark.rb', line 309

def self.bench name, &block
  define_method "bench_#{name.gsub(/\W+/, '_')}", &block
end

.bench_performance_constant(name, threshold = 0.99, &work) ⇒ Object

Create a benchmark that verifies that the performance is constant.

describe "my class" do
  bench_performance_constant "zoom_algorithm!" do |n|
    @obj.zoom_algorithm!(n)
  end
end

353
354
355
356
357
# File 'lib/minitest/benchmark.rb', line 353

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

Create a benchmark that verifies that the performance is exponential.

describe "my class" do
  bench_performance_exponential "algorithm" do |n|
    @obj.algorithm(n)
  end
end

368
369
370
371
372
# File 'lib/minitest/benchmark.rb', line 368

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

Create a benchmark that verifies that the performance is linear.

describe "my class" do
  bench_performance_linear "fast_algorithm", 0.9999 do |n|
    @obj.fast_algorithm(n)
  end
end

338
339
340
341
342
# File 'lib/minitest/benchmark.rb', line 338

def self.bench_performance_linear name, threshold = 0.99, &work
  bench name do
    assert_performance_linear threshold, &work
  end
end

.bench_range(&block) ⇒ Object

Specifies the ranges used for benchmarking for that class.

bench_range do
  bench_exp(2, 16, 2)
end

See Unit::TestCase.bench_range for more details.


322
323
324
325
326
327
# File 'lib/minitest/benchmark.rb', line 322

def self.bench_range &block
  return super unless block

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

.childrenObject

Returns the children of this spec.


141
142
143
# File 'lib/minitest/spec.rb', line 141

def self.children
  @children ||= []
end

.create(name, desc) ⇒ Object

:nodoc:


227
228
229
230
231
232
233
234
235
236
237
238
# File 'lib/minitest/spec.rb', line 227

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

    nuke_test_methods!
  end

  children << cls

  cls
end

.describe_stackObject

:nodoc:


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

def self.describe_stack # :nodoc:
  @@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 inheritence, 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/unit. You can mix and match between assertions and expectations as much as you want.


190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
# File 'lib/minitest/spec.rb', line 190

def self.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.


212
213
214
215
216
217
# File 'lib/minitest/spec.rb', line 212

def self.let name, &block
  define_method name do
    @_memoized ||= {}
    @_memoized.fetch(name) { |k| @_memoized[k] = instance_eval(&block) }
  end
end

.nuke_test_methods!Object

:nodoc:


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

def self.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

109
110
111
112
113
114
115
116
# File 'lib/minitest/spec.rb', line 109

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

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

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

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

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

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


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

def self.subject &block
  let :subject, &block
end

.to_sObject Also known as: name

:nodoc:


240
241
242
# File 'lib/minitest/spec.rb', line 240

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