Sha256: 35fcebad34d2d1a4bed6469e2af16b5fd94546d6bb297e1dfe36cd0094334fcf

Contents?: true

Size: 1.3 KB

Versions: 6

Compression:

Stored size: 1.3 KB

Contents

module RR
  # RR::ProbeCreator uses RR::ProbeCreator#method_missing to create
  # a Scenario that acts like a probe.
  #
  # The following example probes method_name with arg1 and arg2
  # returning the actual value of the method. The block is an after callback
  # that intercepts the return value. Mocks or other modifications can
  # be done to the return value.
  #
  #   probe(subject).method_name(arg1, arg2) { |return_value| }
  #
  # The ProbeCreator also supports a block sytnax. The block accepts
  # a after_call callback, instead of a return value as with MockCreator
  # and StubCreator.
  #
  #    probe(User) do |m|
  #      m.find('4') do |user|
  #        mock(user).valid? {false}
  #      end
  #    end
  #
  #   user = User.find('4')
  #   user.valid? # false
  class ProbeCreator
    instance_methods.each { |m| undef_method m unless m =~ /^__/ }
    
    def initialize(space, subject)
      @space = space
      @subject = subject
      yield(self) if block_given?
    end

    protected
    def method_missing(method_name, *args, &after_call)
      double = @space.create_double(@subject, method_name)
      scenario = @space.create_scenario(double)
      scenario.with(*args).once.implemented_by(double.original_method)
      scenario.after_call(&after_call) if after_call
      scenario
    end
  end
end

Version data entries

6 entries across 6 versions & 1 rubygems

Version Path
rr-0.1.8 lib/rr/probe_creator.rb
rr-0.1.7 lib/rr/probe_creator.rb
rr-0.1.10 lib/rr/probe_creator.rb
rr-0.1.11 lib/rr/probe_creator.rb
rr-0.1.6 lib/rr/probe_creator.rb
rr-0.1.9 lib/rr/probe_creator.rb