Parent

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[R]

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/clr/isolation.rb, line 10
      def for(subject, recorder = MethodCallRecorder.new, expectations = Expectations.new)
        context = IsolatorContext.new subject, recorder, expectations
        isolation_strategy = subject.is_clr_type? ? get_clr_isolation_strategy(subject) : RubyIsolator

        isolator = isolation_strategy.for context
        isolation = new(isolator, context)
        isolator.isolation
      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/isolation.rb, line 113
      def for(subject, recorder = MethodCallRecorder.new, expectations = Expectations.new)
        context = IsolatorContext.new subject, recorder, expectations

        isolator = RubyIsolator.for context
        isolation = new(isolator, context)
        isolator.isolation
      end
new(isolator, context) click to toggle source

Initializes a new instance of this isolation.

# File lib/caricature/isolation.rb, line 65
    def initialize(isolator, context)
      @instance = isolator.subject
      @recorder = context.recorder
      @messenger = context.messenger
      @expectations = context.expectations
      isolator.isolation.class.instance_variable_set("@___context___", self)
    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 24
        def get_clr_isolation_strategy(subject)
          return ClrInterfaceIsolator if subject.respond_to? :class_eval and !subject.respond_to? :new
          ClrIsolator
        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
    def class_verify(method_name, &block)
      internal_verify method_name, :class, &block
    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 93
    def create_class_override(method_name, &block)
      puts "creating override"
      internal_create_override method_name, :class, &block      
    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 88
    def create_override(method_name, &block)
      internal_create_override method_name, :instance, &block
    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 82
    def send_class_message(method_name, return_type, *args, &b)
      recorder.record_call method_name, :class, *args, &b
      @messenger.deliver_to_class(method_name, return_type, *args, &b)
    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 75
    def send_message(method_name, return_type, *args, &b)
      recorder.record_call method_name, :instance, *args, &b
      @messenger.deliver(method_name, return_type, *args, &b)
    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
    def verify(method_name, &block)
      internal_verify method_name, :instance, &block
    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
    def internal_create_override(method_name, mode=:instance, &block)
      builder = ExpectationBuilder.new method_name
      block.call builder unless block.nil?
      exp = builder.build
      expectations.add_expectation exp, mode
      exp
    end
internal_verify(method_name, mode=:instance, &block) click to toggle source

(Not documented)

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

Disabled; run with --debug to generate this.

[Validate]

Generated with the Darkfish Rdoc Generator 1.1.6.