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 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54
|
require 'mocha/parameter_matchers/base'
require 'yaml'
module Mocha
module ParameterMatchers
# Matches any object that responds to +message+ with +result+. To put it another way, it tests the quack, not the duck.
#
# @param [Symbol] message method to invoke.
# @param [Object] result expected result of sending +message+.
# @return [RespondsWith] parameter matcher.
#
# @see Expectation#with
#
# @example Actual parameter responds with "FOO" when :upcase is invoked.
# object = mock()
# object.expects(:method_1).with(responds_with(:upcase, "FOO"))
# object.method_1("foo")
# # no error raised, because "foo".upcase == "FOO"
#
# @example Actual parameter does not respond with "FOO" when :upcase is invoked.
# object = mock()
# object.expects(:method_1).with(responds_with(:upcase, "BAR"))
# object.method_1("foo")
# # error raised, because "foo".upcase != "BAR"
def responds_with(message, result)
RespondsWith.new(message, result)
end
# Parameter matcher which matches if actual parameter returns expected result when specified method is invoked.
class RespondsWith < Base
# @private
def initialize(message, result)
@message, @result = message, result
end
# @private
def matches?(available_parameters)
parameter = available_parameters.shift
parameter.__send__(@message) == @result
end
# @private
def mocha_inspect
"responds_with(#{@message.mocha_inspect}, #{@result.mocha_inspect})"
end
end
end
end
|