Class: Caricature::Isolation

Inherits:
Object
  • Object
show all
Defined in:
lib/caricature/isolation.rb,
lib/caricature/clr/isolation.rb

Overview

:nodoc:

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

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

#eventsObject (readonly)

the event subscriptions collected at runtime



8
9
10
# File 'lib/caricature/clr/isolation.rb', line 8

def events
  @events
end

#expectationsObject (readonly)

the expecations set for this isolation



62
63
64
# File 'lib/caricature/isolation.rb', line 62

def expectations
  @expectations
end

#instanceObject

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

#recorderObject (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 send_class_message(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 send_message(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