Sha256: 4832ddaf82fc196c4ae28ef25c67fab2ec08718483d8dd36df4cb6486294414e

Contents?: true

Size: 896 Bytes

Versions: 12

Compression:

Stored size: 896 Bytes

Contents

module RR
  # RR::MockCreator uses RR::MockCreator#method_missing to create
  # a Scenario that acts like a mock.
  #
  # The following example mocks method_name with arg1 and arg2
  # returning return_value.
  #
  #   mock(subject).method_name(arg1, arg2) { return_value }
  #
  # The MockCreator also supports a block sytnax.
  #
  #    mock(subject) do |m|
  #      m.method_name(arg1, arg2) { return_value }
  #    end
  class MockCreator
    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.with(*args).once.returns(&returns)
      scenario
    end
  end
end

Version data entries

12 entries across 12 versions & 1 rubygems

Version Path
rr-0.1.10 lib/rr/mock_creator.rb
rr-0.1.11 lib/rr/mock_creator.rb
rr-0.1.4 lib/rr/mock_creator.rb
rr-0.1.8 lib/rr/mock_creator.rb
rr-0.1.0 lib/rr/mock_creator.rb
rr-0.1.3 lib/rr/mock_creator.rb
rr-0.1.7 lib/rr/mock_creator.rb
rr-0.1.6 lib/rr/mock_creator.rb
rr-0.1.1 lib/rr/mock_creator.rb
rr-0.1.5 lib/rr/mock_creator.rb
rr-0.1.2 lib/rr/mock_creator.rb
rr-0.1.9 lib/rr/mock_creator.rb