File: mock.rb

package info (click to toggle)
jruby 1.5.1-1%2Bdeb6u1
  • links: PTS, VCS
  • area: non-free
  • in suites: squeeze-lts
  • size: 47,024 kB
  • ctags: 74,144
  • sloc: ruby: 398,155; java: 169,506; yacc: 3,782; xml: 2,469; ansic: 415; sh: 279; makefile: 78; tcl: 40
file content (37 lines) | stat: -rw-r--r-- 1,162 bytes parent folder | download | duplicates (4)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
############################################################
# This file is imported from a different project.
# DO NOT make modifications in this repo.
# File a patch instead and assign it to Ryan Davis
############################################################

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__(: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