Sha256: 48cfa1979820999a264d26acacc8f268b2884a3884fa9ee14a836cf59edbabfe

Contents?: true

Size: 963 Bytes

Versions: 4

Compression:

Stored size: 963 Bytes

Contents

class MockExpectationError < StandardError; end

module MiniTest
  class Mock
    def initialize
      @expected_calls = {}
      @actual_calls = Hash.new {|h,k| h[k] = [] }
    end

    def expect(name, retval, args=[])
      n, r, a = name, retval, args # for the closure below
      @expected_calls[name] = { :retval => retval, :args => args }
      self.class.__send__ :remove_method, name if respond_to? name
      self.class.__send__(:define_method, name) { |*x|
        raise ArgumentError unless @expected_calls[n][:args].size == x.size
        @actual_calls[n] << { :retval => r, :args => x }
        retval
      }
      self
    end

    def verify
      @expected_calls.each_key do |name|
        expected = @expected_calls[name]
        msg = "expected #{name}, #{expected.inspect}"
        raise MockExpectationError, msg unless
          @actual_calls.has_key? name and @actual_calls[name].include?(expected)
      end
      true
    end
  end
end

Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
minitest-1.7.2 lib/minitest/mock.rb
minitest-1.7.1 lib/minitest/mock.rb
minitest-1.7.0 lib/minitest/mock.rb
minitest-1.6.0 lib/minitest/mock.rb