Sha256: 04679bd5cb0e75336b494f579dfd663348f088f8013881a9e3e78e15703ee822

Contents?: true

Size: 966 Bytes

Versions: 9

Compression:

Stored size: 966 Bytes

Contents

module RR
  # RR::StubCreator uses RR::StubCreator#method_missing to create
  # a Scenario that acts like a stub.
  #
  # The following example stubs method_name with arg1 and arg2
  # returning return_value.
  #
  #   stub(subject).method_name(arg1, arg2) { return_value }
  #
  # The StubCreator also supports a block sytnax.
  #
  #    stub(subject) do |m|
  #      m.method_name(arg1, arg2) { return_value }
  #    end
  class StubCreator
    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, &returns)
      double = @space.create_double(@subject, method_name)
      scenario = @space.create_scenario(double)
      scenario.returns(&returns)
      if args.empty?
        scenario.with_any_args
      else
        scenario.with(*args)
      end
    end
  end
end

Version data entries

9 entries across 9 versions & 1 rubygems

Version Path
rr-0.1.1 lib/rr/stub_creator.rb
rr-0.1.4 lib/rr/stub_creator.rb
rr-0.1.2 lib/rr/stub_creator.rb
rr-0.1.6 lib/rr/stub_creator.rb
rr-0.1.0 lib/rr/stub_creator.rb
rr-0.1.5 lib/rr/stub_creator.rb
rr-0.1.7 lib/rr/stub_creator.rb
rr-0.1.8 lib/rr/stub_creator.rb
rr-0.1.3 lib/rr/stub_creator.rb