Class: Caricature::Isolation
- Defined in:
- lib/caricature/isolation.rb,
lib/caricature/clr/isolation.rb
Overview
:nodoc:
Instance Attribute Summary collapse
-
#events ⇒ Object
readonly
the event subscriptions collected at runtime.
-
#expectations ⇒ Object
readonly
the expecations set for this isolation.
-
#instance ⇒ Object
the real instance of the isolated subject used to forward calls in partial mocks.
-
#recorder ⇒ Object
readonly
the method call recorder.
Class Method Summary collapse
-
.for(subject, recorder = MethodCallRecorder.new, expectations = Expectations.new) ⇒ Object
Creates an isolation object complete with proxy and method call recorder It works out which isolation it needs to create and provide and initializes the method call recorder.
Instance Method Summary collapse
-
#add_event_subscription(event_name, mode, handler) ⇒ Object
adds an event subscription.
-
#class_verify(method_name, &block) ⇒ Object
asserts whether the method has been called for the specified configuration.
-
#create_class_override(method_name, &block) ⇒ Object
builds up an expectation for a class method, allows for overriding the result returned by the class method.
-
#create_override(method_name, &block) ⇒ Object
builds up an expectation for an instance method, allows for overriding the result returned by the method.
-
#initialize(isolator, context) ⇒ Isolation
constructor
Initializes a new instance of this isolation.
-
#internal_create_override(method_name, mode = :instance, &block) ⇒ Object
def add_event_expectation(name, mode, *args, &handler) end.
-
#remove_event_subscription(event_name, mode, handler) ⇒ Object
removes an event subscription.
-
#send_class_message(method_name, return_type, *args, &b) ⇒ Object
record and send the message to the isolation.
-
#send_message(method_name, return_type, *args, &b) ⇒ Object
record and send the message to the isolation.
-
#verify(method_name, &block) ⇒ Object
asserts whether the method has been called for the specified configuration.
- #verify_event_raise(event_name, mode = :instance, &block) ⇒ Object
Constructor Details
#initialize(isolator, context) ⇒ Isolation
Initializes a new instance of this isolation.
65 66 67 68 69 70 71 72 |
# File 'lib/caricature/isolation.rb', line 65 def initialize(isolator, context) @instance = isolator.subject @messenger = context.messenger @messenger.recorder = @recorder = context.recorder @expectations = context.expectations @proxy = isolator.isolation isolator.isolation.class.instance_variable_set("@___context___", self) end |
Instance Attribute Details
#events ⇒ Object (readonly)
the event subscriptions collected at runtime
8 9 10 |
# File 'lib/caricature/clr/isolation.rb', line 8 def events @events end |
#expectations ⇒ Object (readonly)
the expecations set for this isolation
62 63 64 |
# File 'lib/caricature/isolation.rb', line 62 def expectations @expectations end |
#instance ⇒ Object
the real instance of the isolated subject used to forward calls in partial mocks
56 57 58 |
# File 'lib/caricature/isolation.rb', line 56 def instance @instance end |
#recorder ⇒ Object (readonly)
the method call recorder
59 60 61 |
# File 'lib/caricature/isolation.rb', line 59 def recorder @recorder end |
Class Method Details
.for(subject, recorder = MethodCallRecorder.new, expectations = Expectations.new) ⇒ Object
Creates an isolation object complete with proxy and method call recorder It works out which isolation it needs to create and provide and initializes the method call recorder
111 112 113 114 115 116 117 |
# File 'lib/caricature/isolation.rb', line 111 def for(subject, recorder = MethodCallRecorder.new, expectations = Expectations.new) context = IsolatorContext.new subject, recorder, expectations isolator = RubyIsolator.for context isolation = new(isolator, context) isolation end |
Instance Method Details
#add_event_subscription(event_name, mode, handler) ⇒ Object
adds an event subscription
16 17 18 19 20 21 |
# File 'lib/caricature/clr/isolation.rb', line 16 def add_event_subscription(event_name, mode, handler) events[mode] ||= {} nm = event_name_for(event_name) events[mode][nm] ||= [] events[mode][nm] << handler end |
#class_verify(method_name, &block) ⇒ Object
asserts whether the method has been called for the specified configuration
102 103 104 |
# File 'lib/caricature/isolation.rb', line 102 def class_verify(method_name, &block) internal_verify method_name, :class, &block end |
#create_class_override(method_name, &block) ⇒ Object
builds up an expectation for a class method, allows for overriding the result returned by the class method
92 93 94 |
# File 'lib/caricature/isolation.rb', line 92 def create_class_override(method_name, &block) internal_create_override method_name, :class, &block end |
#create_override(method_name, &block) ⇒ Object
builds up an expectation for an instance method, allows for overriding the result returned by the method
87 88 89 |
# File 'lib/caricature/isolation.rb', line 87 def create_override(method_name, &block) internal_create_override method_name, :instance, &block end |
#internal_create_override(method_name, mode = :instance, &block) ⇒ Object
def add_event_expectation(name, mode, *args, &handler) end
31 32 33 34 35 36 37 |
# File 'lib/caricature/clr/isolation.rb', line 31 def internal_create_override(method_name, mode=:instance, &block) builder = ExpectationBuilder.new method_name block.call builder if block exp = builder.build expectations.add_expectation exp, mode exp end |
#remove_event_subscription(event_name, mode, handler) ⇒ Object
removes an event subscription
24 25 26 |
# File 'lib/caricature/clr/isolation.rb', line 24 def remove_event_subscription(event_name, mode, handler) ((events[mode] ||= {})[event_name_for(event_name)] ||=[]).delete(handler) end |
#send_class_message(method_name, return_type, *args, &b) ⇒ Object
record and send the message to the isolation. takes care of following expectations rules when sending messages.
82 83 84 |
# File 'lib/caricature/isolation.rb', line 82 def (method_name, return_type, *args, &b) @messenger.deliver_to_class(method_name, return_type, *args, &b) end |
#send_message(method_name, return_type, *args, &b) ⇒ Object
record and send the message to the isolation. takes care of following expectations rules when sending messages.
76 77 78 |
# File 'lib/caricature/isolation.rb', line 76 def (method_name, return_type, *args, &b) @messenger.deliver(method_name, return_type, *args, &b) end |
#verify(method_name, &block) ⇒ Object
asserts whether the method has been called for the specified configuration
97 98 99 |
# File 'lib/caricature/isolation.rb', line 97 def verify(method_name, &block) internal_verify method_name, :instance, &block end |
#verify_event_raise(event_name, mode = :instance, &block) ⇒ Object
40 41 42 43 44 |
# File 'lib/caricature/clr/isolation.rb', line 40 def verify_event_raise(event_name, mode= :instance, &block) verification = EventVerification.new(event_name, recorder, mode) block.call verification unless block.nil? verification end |