Parent

Files

Caricature::Isolation

Instead of using confusing terms like Mocking and Stubbing which is basically the same. Caricature tries to unify those concepts by using a term of Isolation. When you’re testing you typically want to be in control of what you’re testing. To do that you isolate dependencies and possibly define return values for methods on them. At a later stage you might be interested in which method was called, maybe even with which parameters or you might be interested in the amount of times it has been called.

Attributes

instance[RW]

the real instance of the isolated subject used to forward calls in partial mocks

recorder[R]

the method call recorder

expectations[R]

the expecations set for this isolation

Public Class Methods

for(subject, recorder = MethodCallRecorder.new, expectations = Expectations.new) click to toggle source

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

     # File lib/caricature/isolation.rb, line 113
113:       def for(subject, recorder = MethodCallRecorder.new, expectations = Expectations.new)
114:         context = IsolatorContext.new subject, recorder, expectations
115: 
116:         isolator = RubyIsolator.for context
117:         isolation = new(isolator, context)
118:         isolator.isolation
119:       end
for(subject, recorder = MethodCallRecorder.new, expectations = Expectations.new) click to toggle source

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

    # File lib/caricature/clr/isolation.rb, line 19
19:       def for(subject, recorder = MethodCallRecorder.new, expectations = Expectations.new)
20:         context = IsolatorContext.new subject, recorder, expectations
21:         isolation_strategy = subject.is_clr_type? ? get_clr_isolation_strategy(subject) : RubyIsolator
22: 
23:         isolator = isolation_strategy.for context
24:         isolation = new(isolator, context)
25:         isolator.isolation
26:       end
new(isolator, context) click to toggle source

Initializes a new instance of this isolation.

    # File lib/caricature/isolation.rb, line 65
65:     def initialize(isolator, context)
66:       @instance = isolator.subject
67:       @recorder = context.recorder
68:       @messenger = context.messenger
69:       @expectations = context.expectations 
70:       @proxy = isolator.isolation
71:       isolator.isolation.class.instance_variable_set("@___context___", self)
72:     end

Private Class Methods

get_clr_isolation_strategy(subject) click to toggle source

decides which startegy to use for mocking a CLR object. When the provided subject is an interface it will return a ClrInterfaceIsolator otherwise it will return a ClrIsolator

    # File lib/caricature/clr/isolation.rb, line 33
33:         def get_clr_isolation_strategy(subject)
34:           return ClrInterfaceIsolator if subject.respond_to? :class_eval and !subject.respond_to? :new
35:           ClrIsolator
36:         end

Public Instance Methods

class_verify(method_name, &block) click to toggle source

asserts whether the method has been called for the specified configuration

     # File lib/caricature/isolation.rb, line 104
104:     def class_verify(method_name, &block)
105:       internal_verify method_name, :class, &block
106:     end
create_class_override(method_name, &block) click to toggle source

builds up an expectation for a class method, allows for overriding the result returned by the class method

    # File lib/caricature/isolation.rb, line 94
94:     def create_class_override(method_name, &block)
95:       internal_create_override method_name, :class, &block      
96:     end
create_override(method_name, &block) click to toggle source

builds up an expectation for an instance method, allows for overriding the result returned by the method

    # File lib/caricature/isolation.rb, line 89
89:     def create_override(method_name, &block)
90:       internal_create_override method_name, :instance, &block
91:     end
internal_create_override(method_name, mode=:instance, &block) click to toggle source

(Not documented)

    # File lib/caricature/clr/isolation.rb, line 5
 5:     def internal_create_override(method_name, mode=:instance, &block)
 6:       builder = ExpectationBuilder.new method_name
 7:       block.call builder unless block.nil?
 8:       exp = builder.build           
 9:       
10:       expectations.add_expectation exp, mode
11:       exp
12:     end
send_class_message(method_name, return_type, *args, &b) click to toggle source

record and send the message to the isolation. takes care of following expectations rules when sending messages.

    # File lib/caricature/isolation.rb, line 83
83:     def send_class_message(method_name, return_type, *args, &b)
84:       recorder.record_call method_name, :class, *args, &b
85:       @messenger.deliver_to_class(method_name, return_type, *args, &b)
86:     end
send_message(method_name, return_type, *args, &b) click to toggle source

record and send the message to the isolation. takes care of following expectations rules when sending messages.

    # File lib/caricature/isolation.rb, line 76
76:     def send_message(method_name, return_type, *args, &b)
77:       recorder.record_call method_name, :instance, *args, &b
78:       @messenger.deliver(method_name, return_type, *args, &b)
79:     end
verify(method_name, &block) click to toggle source

asserts whether the method has been called for the specified configuration

     # File lib/caricature/isolation.rb, line 99
 99:     def verify(method_name, &block)
100:       internal_verify method_name, :instance, &block
101:     end

Protected Instance Methods

internal_create_override(method_name, mode=:instance, &block) click to toggle source

(Not documented)

     # File lib/caricature/isolation.rb, line 125
125:     def internal_create_override(method_name, mode=:instance, &block)
126:       builder = ExpectationBuilder.new method_name
127:       block.call builder unless block.nil?
128:       exp = builder.build           
129:       @proxy.class.send((mode == :instance ? :define_method : :define_cmethod), method_name.to_sym, lambda do  |*args|  
130:         b = nil
131:         b = Proc.new { yield } if block_given?
132:         isolation_context.send_message(method_name, nil, *args, &b)
133:       end)
134:       expectations.add_expectation exp, mode
135:       exp
136:     end
internal_verify(method_name, mode=:instance, &block) click to toggle source

(Not documented)

     # File lib/caricature/isolation.rb, line 138
138:     def internal_verify(method_name, mode=:instance, &block)
139:       verification = Verification.new(method_name, recorder, mode)
140:       block.call verification unless block.nil?
141:       verification
142:     end

Disabled; run with --debug to generate this.

[Validate]

Generated with the Darkfish Rdoc Generator 1.1.6.