Sha256: 1f0b3b8b4489e979b73a0200a9e873a6656617382d67f4a1cd8185fd06bdfeeb

Contents?: true

Size: 1.6 KB

Versions: 29

Compression:

Stored size: 1.6 KB

Contents

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 = message
        @result = result
      end

      # @private
      def matches?(available_parameters)
        parameter = available_parameters.shift
        @result.to_matcher.matches?([parameter.__send__(@message)])
      end

      # @private
      def mocha_inspect
        "responds_with(#{@message.mocha_inspect}, #{@result.mocha_inspect})"
      end
    end
  end
end

Version data entries

29 entries across 29 versions & 3 rubygems

Version Path
mocha-2.1.0 lib/mocha/parameter_matchers/responds_with.rb
mocha-2.0.4 lib/mocha/parameter_matchers/responds_with.rb
mocha-2.0.3 lib/mocha/parameter_matchers/responds_with.rb
fluent-plugin-google-cloud-logging-on-prem-0.1.0 vendor/ruby/3.1.0/gems/mocha-2.0.2/lib/mocha/parameter_matchers/responds_with.rb
mocha-2.0.2 lib/mocha/parameter_matchers/responds_with.rb
mocha-1.16.1 lib/mocha/parameter_matchers/responds_with.rb
mocha-1.15.1 lib/mocha/parameter_matchers/responds_with.rb
mocha-2.0.1 lib/mocha/parameter_matchers/responds_with.rb
mocha-2.0.0 lib/mocha/parameter_matchers/responds_with.rb
mocha-1.16.0 lib/mocha/parameter_matchers/responds_with.rb
mocha-2.0.0.alpha.1 lib/mocha/parameter_matchers/responds_with.rb
mocha-2.0.0.alpha lib/mocha/parameter_matchers/responds_with.rb
mocha-1.15.0 lib/mocha/parameter_matchers/responds_with.rb
mocha-1.14.0 lib/mocha/parameter_matchers/responds_with.rb
mocha-1.13.0 lib/mocha/parameter_matchers/responds_with.rb
mocha-1.12.0 lib/mocha/parameter_matchers/responds_with.rb
mocha-1.11.2 lib/mocha/parameter_matchers/responds_with.rb
mocha-1.11.1 lib/mocha/parameter_matchers/responds_with.rb
mocha-1.11.0 lib/mocha/parameter_matchers/responds_with.rb
mocha-1.10.2 lib/mocha/parameter_matchers/responds_with.rb