Sha256: b464e094bc1f9c4208e5e68394c1a145600eca5a720b950a4d1d12c8c9134984

Contents?: true

Size: 1.6 KB

Versions: 13

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

13 entries across 10 versions & 2 rubygems

Version Path
mocha-1.6.0 lib/mocha/parameter_matchers/responds_with.rb
mocha-1.5.0 lib/mocha/parameter_matchers/responds_with.rb
mocha-1.4.0 lib/mocha/parameter_matchers/responds_with.rb
mocha-1.3.0 lib/mocha/parameter_matchers/responds_with.rb
mocha-1.2.1 lib/mocha/parameter_matchers/responds_with.rb
mocha-1.2.0 lib/mocha/parameter_matchers/responds_with.rb
apl-library-0.0.90 vendor/bundle/ruby/2.1.0/gems/mocha-1.0.0/lib/mocha/parameter_matchers/responds_with.rb
apl-library-0.0.90 vendor/bundle/ruby/1.9.1/gems/mocha-1.0.0/lib/mocha/parameter_matchers/responds_with.rb
apl-library-0.0.90 vendor/bundle/ruby/2.1.0/gems/apl-library-0.0.90/vendor/bundle/ruby/1.9.1/gems/mocha-1.0.0/lib/mocha/parameter_matchers/responds_with.rb
apl-library-0.0.90 vendor/bundle/ruby/2.1.0/gems/apl-library-0.0.90/vendor/bundle/ruby/2.1.0/gems/mocha-1.0.0/lib/mocha/parameter_matchers/responds_with.rb
mocha-1.1.0 lib/mocha/parameter_matchers/responds_with.rb
mocha-1.0.0 lib/mocha/parameter_matchers/responds_with.rb
mocha-1.0.0.alpha lib/mocha/parameter_matchers/responds_with.rb