Class: Caricature::Isolator
Overview
A base class for Isolator
objects to stick with the Isolation
nomenclature the strategies for creating isolations are called isolators. An isolator functions as a barrier between the code in your test and the underlying type/instance. It allows you to take control over the value that is returned from a specific method, if you want to pass the method call along to the underlying instance etc. It also contains the ability to verify if a method was called, with which arguments etc.
Direct Known Subclasses
Instance Attribute Summary collapse
-
#descriptor ⇒ Object
readonly
holds the descriptor for this type of object.
-
#isolation ⇒ Object
readonly
holds the isolation created by this isolator.
-
#subject ⇒ Object
readonly
holds the subject of this isolator.
Class Method Summary collapse
-
.for(context) ⇒ Object
Creates the actual proxy object for the
subject
and initializes it with arecorder
andexpectations
This is the actual isolation that will be used to in your tests.
Instance Method Summary collapse
-
#build_isolation(klass, inst = nil) ⇒ Object
builds up the isolation class instance.
-
#class_name(subj) ⇒ Object
Creates the new class name for the isolation.
-
#initialize(context) ⇒ Isolator
constructor
creates a new instance of an isolator.
-
#initialize_isolation(klass, context) ⇒ Object
Sets up the necessary instance variables for the isolation.
-
#initialize_messenger ⇒ Object
initializes the messaging strategy for the isolator.
Constructor Details
#initialize(context) ⇒ Isolator
creates a new instance of an isolator
187 188 189 |
# File 'lib/caricature/isolator.rb', line 187 def initialize(context) @context = context end |
Instance Attribute Details
#descriptor ⇒ Object (readonly)
holds the descriptor for this type of object
184 185 186 |
# File 'lib/caricature/isolator.rb', line 184 def descriptor @descriptor end |
#isolation ⇒ Object (readonly)
holds the isolation created by this isolator
178 179 180 |
# File 'lib/caricature/isolator.rb', line 178 def isolation @isolation end |
#subject ⇒ Object (readonly)
holds the subject of this isolator
181 182 183 |
# File 'lib/caricature/isolator.rb', line 181 def subject @subject end |
Class Method Details
.for(context) ⇒ Object
Creates the actual proxy object for the subject
and initializes it with a recorder
and expectations
This is the actual isolation that will be used to in your tests. It implements all the methods of the subject
so as long as you’re in Ruby and just need to isolate out some classes defined in a statically compiled language it should get you all the way there for public instance methods at this point. when you’re going to isolation for usage within a statically compiled language type then you’re bound to most of their rules. So you need to either isolate interfaces or mark the methods you want to isolate as virtual in your implementing classes.
229 230 231 232 233 |
# File 'lib/caricature/isolator.rb', line 229 def for(context) context.recorder ||= MethodCallRecorder.new context.expectations ||= Expectations.new new(context) end |
Instance Method Details
#build_isolation(klass, inst = nil) ⇒ Object
builds up the isolation class instance
192 193 194 195 196 197 |
# File 'lib/caricature/isolator.rb', line 192 def build_isolation(klass, inst=nil) pxy = create_isolation_for klass @isolation = pxy.new @subject = inst initialize_messenger end |
#class_name(subj) ⇒ Object
Creates the new class name for the isolation
205 206 207 208 209 |
# File 'lib/caricature/isolator.rb', line 205 def class_name(subj) nm = subj.respond_to?(:class_eval) ? subj.demodulize : subj.class.demodulize @class_name = "#{nm}#{UUIDTools::UUID.random_create.to_s.gsub /-/, ''}" @class_name end |
#initialize_isolation(klass, context) ⇒ Object
Sets up the necessary instance variables for the isolation
212 213 214 215 216 |
# File 'lib/caricature/isolator.rb', line 212 def initialize_isolation(klass, context) pxy = klass.new pxy.instance_variable_set("@___context___", context) pxy end |
#initialize_messenger ⇒ Object
initializes the messaging strategy for the isolator
200 201 202 |
# File 'lib/caricature/isolator.rb', line 200 def initialize_messenger raise NotImplementedError end |