Module: MiniTest::Spec::DSL
- Included in:
- 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]]
- @@describe_stack =
[]
Instance Attribute Summary collapse
-
#desc ⇒ Object
readonly
:stopdoc:.
Instance Method Summary collapse
-
#after(type = nil, &block) ⇒ Object
Define an ‘after’ action.
-
#before(type = nil, &block) ⇒ Object
Define a ‘before’ action.
-
#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
Essentially, define an accessor for
name
withblock
. -
#name ⇒ Object
:nodoc:.
-
#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
Another lazy man’s accessor generator.
-
#to_s ⇒ Object
:nodoc:.
Instance Attribute Details
#desc ⇒ Object (readonly)
:stopdoc:
254 255 256 |
# File 'lib/minitest/spec.rb', line 254 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::Unit::TestCase#teardown.
177 178 179 180 181 182 |
# File 'lib/minitest/spec.rb', line 177 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::Unit::TestCase#setup.
163 164 165 166 167 168 |
# File 'lib/minitest/spec.rb', line 163 def before type = nil, &block define_method :setup do super() self.instance_eval(&block) end end |
#children ⇒ Object
Returns the children of this spec.
146 147 148 |
# File 'lib/minitest/spec.rb', line 146 def children @children ||= [] end |
#create(name, desc) ⇒ Object
:nodoc:
232 233 234 235 236 237 238 239 240 241 242 243 |
# File 'lib/minitest/spec.rb', line 232 def 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:
139 140 141 |
# File 'lib/minitest/spec.rb', line 139 def 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 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/unit. You can mix and match between assertions and expectations as much as you want.
195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 |
# File 'lib/minitest/spec.rb', line 195 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.
217 218 219 220 221 222 |
# File 'lib/minitest/spec.rb', line 217 def let name, &block define_method name do @_memoized ||= {} @_memoized.fetch(name) { |k| @_memoized[k] = instance_eval(&block) } end end |
#name ⇒ Object
:nodoc:
245 246 247 |
# File 'lib/minitest/spec.rb', line 245 def name # :nodoc: defined?(@name) ? @name : super end |
#nuke_test_methods! ⇒ Object
:nodoc:
150 151 152 153 154 |
# File 'lib/minitest/spec.rb', line 150 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
114 115 116 117 118 119 120 121 |
# File 'lib/minitest/spec.rb', line 114 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
128 129 130 131 132 133 134 135 136 |
# File 'lib/minitest/spec.rb', line 128 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
.
228 229 230 |
# File 'lib/minitest/spec.rb', line 228 def subject &block let :subject, &block end |
#to_s ⇒ Object
:nodoc:
249 250 251 |
# File 'lib/minitest/spec.rb', line 249 def to_s # :nodoc: name # Can't alias due to 1.8.7, not sure why end |