Class: MiniTest::Spec
- Inherits:
-
Unit::TestCase
- Object
- Unit::TestCase
- MiniTest::Spec
- 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, Assertions::WINDOZE
Class Attribute Summary collapse
-
.desc ⇒ Object
readonly
Returns the value of attribute desc.
Attributes inherited from Unit::TestCase
Class Method Summary collapse
-
.after(type = :each, &block) ⇒ Object
Define an ‘after’ action.
-
.before(type = :each, &block) ⇒ Object
Define a ‘before’ action.
-
.bench(name, &block) ⇒ Object
This is used to define a new benchmark method.
-
.bench_performance_constant(name, threshold = 0.99, &work) ⇒ Object
Create a benchmark that verifies that the performance is constant.
-
.bench_performance_exponential(name, threshold = 0.99, &work) ⇒ Object
Create a benchmark that verifies that the performance is exponential.
-
.bench_performance_linear(name, threshold = 0.99, &work) ⇒ Object
Create a benchmark that verifies that the performance is linear.
- .bench_range(&block) ⇒ Object
-
.children ⇒ Object
Returns the children of this spec.
-
.create(name, desc) ⇒ Object
:nodoc:.
-
.describe_stack ⇒ Object
:nodoc:.
-
.it(desc = "anonymous", &block) ⇒ Object
(also: specify)
Define an expectation with name
desc
. - .let(name, &block) ⇒ Object
-
.nuke_test_methods! ⇒ Object
:nodoc:.
-
.register_spec_type(*args, &block) ⇒ Object
Register a new type of spec that matches the spec’s description.
-
.spec_type(desc) ⇒ Object
Figure out the spec class to use based on a spec’s description.
- .subject(&block) ⇒ Object
-
.to_s ⇒ Object
(also: name)
:nodoc:.
Methods inherited from Unit::TestCase
add_setup_hook, add_teardown_hook, #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?, #passed?, reset, reset_setup_teardown_hooks, #run, #run_setup_hooks, #run_teardown_hooks, #setup, setup_hooks, #sigma, #teardown, teardown_hooks, test_methods, test_order, test_suites, #validation_for_fit
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, #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
Constructor Details
This class inherits a constructor from MiniTest::Unit::TestCase
Class Attribute Details
.desc ⇒ Object (readonly)
Returns the value of attribute desc.
230 231 232 |
# File 'lib/minitest/spec.rb', line 230 def desc @desc end |
Class Method Details
.after(type = :each, &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.
168 169 170 171 172 |
# File 'lib/minitest/spec.rb', line 168 def self.after type = :each, &block raise "unsupported after type: #{type}" unless type == :each add_teardown_hook {|tc| tc.instance_eval(&block) } end |
.before(type = :each, &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.
155 156 157 158 159 |
# File 'lib/minitest/spec.rb', line 155 def self.before type = :each, &block raise "unsupported before type: #{type}" unless type == :each add_setup_hook {|tc| tc.instance_eval(&block) } 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.
311 312 313 |
# File 'lib/minitest/benchmark.rb', line 311 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
346 347 348 349 350 |
# File 'lib/minitest/benchmark.rb', line 346 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
361 362 363 364 365 |
# File 'lib/minitest/benchmark.rb', line 361 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
331 332 333 334 335 |
# File 'lib/minitest/benchmark.rb', line 331 def self.bench_performance_linear name, threshold = 0.99, &work bench name do assert_performance_linear threshold, &work end end |
.bench_range(&block) ⇒ Object
315 316 317 318 319 320 |
# File 'lib/minitest/benchmark.rb', line 315 def self.bench_range &block return super unless block = (class << self; self; end) .send :define_method, "bench_range", &block end |
.children ⇒ Object
Returns the children of this spec.
138 139 140 |
# File 'lib/minitest/spec.rb', line 138 def self.children @children ||= [] end |
.create(name, desc) ⇒ Object
:nodoc:
211 212 213 214 215 216 217 218 219 220 221 222 |
# File 'lib/minitest/spec.rb', line 211 def self.create name, desc # :nodoc: cls = Class.new(self) do @name = name @desc = desc nuke_test_methods! end children << cls cls end |
.describe_stack ⇒ Object
:nodoc:
131 132 133 |
# File 'lib/minitest/spec.rb', line 131 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.
185 186 187 188 189 190 191 192 193 194 195 196 197 198 |
# File 'lib/minitest/spec.rb', line 185 def self.it desc = "anonymous", &block block ||= proc { skip "(no tests defined)" } @specs ||= 0 @specs += 1 name = "test_%04d_%s" % [ @specs, desc.gsub(/\W+/, '_').downcase ] define_method name, &block self.children.each do |mod| mod.send :undef_method, name if mod.public_method_defined? name end end |
.let(name, &block) ⇒ Object
200 201 202 203 204 205 |
# File 'lib/minitest/spec.rb', line 200 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:
142 143 144 145 146 |
# File 'lib/minitest/spec.rb', line 142 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
106 107 108 109 110 111 112 113 |
# File 'lib/minitest/spec.rb', line 106 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
120 121 122 123 124 125 126 127 128 |
# File 'lib/minitest/spec.rb', line 120 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
207 208 209 |
# File 'lib/minitest/spec.rb', line 207 def self.subject &block let :subject, &block end |
.to_s ⇒ Object Also known as: name
:nodoc:
224 225 226 |
# File 'lib/minitest/spec.rb', line 224 def self.to_s # :nodoc: defined?(@name) ? @name : super end |