Module: XSpec::AssertionContext::Doubles
- Defined in:
- lib/xspec/assertion_contexts.rb
Overview
### Doubles
The doubles module provides test doubles that can be used in-place of real objects.
Defined Under Namespace
Modules: AutoVerify, Strict Classes: ClassReference, Double, InstanceReference, Recorder, Reference, StringReference
Constant Summary collapse
- DoubleFailure =
Class.new(RuntimeError)
Class Method Summary collapse
-
.with(*opts) ⇒ Object
It can be configured with a few 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.
-
#assert_exhausted(obj) ⇒ Object
An assertion is provided to validate that all expected methods were called on a double.
- #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.
-
#expect(obj) ⇒ Object
To set up an expectation on a double, call the expected method an arguments on the proxy object returned by ‘expect`.
-
#instance_double(klass) ⇒ Object
An instance double stands in for an instance of the given class reference, given as a string.
Class Method Details
.with(*opts) ⇒ Object
It can be configured with a few options:
-
‘auto_verify` calls `assert_exhausted` on all created doubles after a
unit of work executes successfully to ensure that all expectations that were set were actually called.
-
‘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.
120 121 122 123 124 125 126 127 128 129 130 131 132 |
# File 'lib/xspec/assertion_contexts.rb', line 120 def self.with(*opts) modules = [self] + opts.map {|x| { auto_verify: AutoVerify, 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.
150 151 152 153 154 155 156 157 158 |
# File 'lib/xspec/assertion_contexts.rb', line 150 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 |
#assert_exhausted(obj) ⇒ Object
An assertion is provided to validate that all expected methods were called on a double.
290 291 292 |
# File 'lib/xspec/assertion_contexts.rb', line 290 def assert_exhausted(obj) obj._verify end |
#call(unit_of_work) ⇒ Object
104 105 106 107 108 |
# File 'lib/xspec/assertion_contexts.rb', line 104 def call(unit_of_work) super rescue DoubleFailure => e [Failure.new(unit_of_work, e., e.backtrace)] end |
#class_double(klass) ⇒ Object
Simarly, a class double validates that class responds to all expected methods, if that class has been loaded.
144 145 146 |
# File 'lib/xspec/assertion_contexts.rb', line 144 def class_double(klass) _double(klass, ClassReference) end |
#expect(obj) ⇒ Object
To set up an expectation on a double, call the expected method an arguments on the proxy object returned by ‘expect`. If a return value is desired, it can be supplied as a block, for example: `expect(double).some_method(1, 2) { “return value” }`
164 165 166 |
# File 'lib/xspec/assertion_contexts.rb', line 164 def expect(obj) Recorder.new(obj) 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.
138 139 140 |
# File 'lib/xspec/assertion_contexts.rb', line 138 def instance_double(klass) _double(klass, InstanceReference) end |