Module: XSpec::Evaluator::Doubles
- Defined in:
- lib/xspec/evaluators.rb
Overview
### Doubles
The doubles module provides test doubles that can be used in-place of real objects.
Defined Under Namespace
Modules: Strict Classes: ClassReference, Double, InstanceReference, Proxy, Reference, StringReference
Class Method Summary collapse
-
.with(*opts) ⇒ Object
It can be configured with the following options:.
Instance Method Summary collapse
-
#_double(klass, type) ⇒ Object
If the doubled class has not been loaded, a null object reference is used that allows expecting of all methods.
- #call(unit_of_work) ⇒ Object
-
#class_double(klass) ⇒ Object
Simarly, a class double validates that class responds to all expected methods, if that class has been loaded.
-
#instance_double(klass) ⇒ Object
An instance double stands in for an instance of the given class reference, given as a string.
-
#stub(obj) ⇒ Object
All messages sent to a double will return ‘nil`.
-
#verify(obj) ⇒ Object
Use ‘verify` to assert that a method was called on a double with particular arguments.
Class Method Details
.with(*opts) ⇒ Object
It can be configured with the following options:
-
‘strict` forbids doubling of classes that have not been loaded. This
should generally be enabled when doing a full spec run, and disabled when running specs in isolation.
The ‘with` method returns a module that can be included in a stack.
109 110 111 112 113 114 115 116 117 118 119 120 |
# File 'lib/xspec/evaluators.rb', line 109 def self.with(*opts) modules = [self] + opts.map {|x| { strict: Strict }.fetch(x) } Module.new do modules.each do |m| include m end end end |
Instance Method Details
#_double(klass, type) ⇒ Object
If the doubled class has not been loaded, a null object reference is used that allows expecting of all methods.
138 139 140 141 142 143 144 145 146 |
# File 'lib/xspec/evaluators.rb', line 138 def _double(klass, type) ref = if self.class.const_defined?(klass) type.new(self.class.const_get(klass)) else StringReference.new(klass) end Double.new(ref) end |
#call(unit_of_work) ⇒ Object
98 99 100 |
# File 'lib/xspec/evaluators.rb', line 98 def call(unit_of_work) super end |
#class_double(klass) ⇒ Object
Simarly, a class double validates that class responds to all expected methods, if that class has been loaded.
132 133 134 |
# File 'lib/xspec/evaluators.rb', line 132 def class_double(klass) _double(klass, ClassReference) end |
#instance_double(klass) ⇒ Object
An instance double stands in for an instance of the given class reference, given as a string. The class does not need to be loaded, but if it is then only public instance methods defined on the class are able to be expected.
126 127 128 |
# File 'lib/xspec/evaluators.rb', line 126 def instance_double(klass) _double(klass, InstanceReference) end |
#stub(obj) ⇒ Object
All messages sent to a double will return ‘nil`. Use `stub` to specify a return value instead: `stub(double).some_method(1, 2) { “return value” }`. This must be called before the message is sent to the double.
160 161 162 |
# File 'lib/xspec/evaluators.rb', line 160 def stub(obj) Proxy.new(obj, :_stub) end |
#verify(obj) ⇒ Object
Use ‘verify` to assert that a method was called on a double with particular arguments. Doubles record all received messages, so `verify` should be called at the end of your test.
151 152 153 |
# File 'lib/xspec/evaluators.rb', line 151 def verify(obj) Proxy.new(obj, :_verify) end |