Skip to content

Commit

Permalink
minor cleanup of Mock#method_missing to improve output and reduce flog
Browse files Browse the repository at this point in the history
[git-p4: depot-paths = "//src/minitest/dev/": change = 6484]
  • Loading branch information
zenspider committed Aug 2, 2011
1 parent 46430ec commit f5ff37a
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 9 deletions.
21 changes: 13 additions & 8 deletions lib/minitest/mock.rb
Expand Up @@ -62,20 +62,25 @@ def verify
end

def method_missing(sym, *args) # :nodoc:
unless @expected_calls.has_key?(sym)
expected = @expected_calls[sym]

unless expected then
raise NoMethodError, "unmocked method %p, expected one of %p" %
[sym, @expected_calls.keys.sort_by(&:to_s)]
end

unless @expected_calls[sym][:args].size == args.size
raise ArgumentError, "mocked method '%s' expects %d arguments, got %d" %
[sym, @expected_calls[sym][:args].size, args.size]
expected_args, retval = expected[:args], expected[:retval]

unless expected_args.size == args.size
raise ArgumentError, "mocked method %p expects %d arguments, got %d" %
[sym, expected[:args].size, args.size]
end

retval = @expected_calls[sym][:retval]
args_case = @expected_calls[sym][:args].
each_with_index.map{|a, i| a if a === args[i]}
@actual_calls[sym] << { :retval => retval, :args => args_case }
@actual_calls[sym] << {
:retval => retval,
:args => expected_args.zip(args).map { |mod, a| mod if mod === a }
}

retval
end

Expand Down
2 changes: 1 addition & 1 deletion test/test_minitest_mock.rb
Expand Up @@ -52,7 +52,7 @@ def test_blow_up_on_wrong_number_of_arguments
@mock.sum
end

assert_equal "mocked method 'sum' expects 2 arguments, got 0", e.message
assert_equal "mocked method :sum expects 2 arguments, got 0", e.message
end

def test_blow_up_on_wrong_arguments
Expand Down

0 comments on commit f5ff37a

Please sign in to comment.