Class: Mini::Mock

Inherits:
Object show all
Defined in:
lib/mini/mock.rb

Instance Method Summary collapse

Constructor Details

#initializeMock

Returns a new instance of Mock.



6
7
8
9
# File 'lib/mini/mock.rb', line 6

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

Instance Method Details

#expect(name, retval, args = []) ⇒ Object



11
12
13
14
15
16
17
18
19
20
# File 'lib/mini/mock.rb', line 11

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__(:define_method, name) { |*a|
    raise ArgumentError unless @expected_calls[n][:args].size == a.size
    @actual_calls[n] << { :retval => r, :args => a }
    retval
  }
  self
end

#verifyObject



22
23
24
25
26
27
28
29
30
# File 'lib/mini/mock.rb', line 22

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