Sha256: caae4c187c57e7fe2338c07d2aee0f075510ae019e49dd443093415b340b2999

Contents?: true

Size: 1.35 KB

Versions: 9

Compression:

Stored size: 1.35 KB

Contents

module Mocktail::Matchers
  # Captors are conceptually complex implementations, but with a simple usage/purpose:
  # They are values the user can create and hold onto that will return a matcher
  # and then "capture" the value made by the real call, for later analysis & assertion.
  #
  # Unlike other matchers, these don't make any useful sense for stubbing, but are
  # very useful when asserting complication call verifications
  #
  # The fact the user will need the reference outside the verification call is
  # why this is a top-level method on Mocktail, and not included in the |m| block
  # arg to stubs/verify
  #
  # See Mockito, which is the earliest implementation I know of:
  # https://javadoc.io/doc/org.mockito/mockito-core/latest/org/mockito/Captor.html
  class Captor
    class Capture < Mocktail::Matchers::Base
      def self.matcher_name
        :capture
      end

      attr_reader :value

      def initialize
        @value = nil
        @captured = false
      end

      def match?(actual)
        @value = actual
        @captured = true
        true
      end

      def captured?
        @captured
      end

      def inspect
        "capture"
      end
    end

    attr_reader :capture
    def initialize
      @capture = Capture.new
    end

    def captured?
      @capture.captured?
    end

    def value
      @capture.value
    end
  end
end

Version data entries

9 entries across 9 versions & 1 rubygems

Version Path
mocktail-1.2.3 lib/mocktail/matchers/captor.rb
mocktail-1.2.2 lib/mocktail/matchers/captor.rb
mocktail-1.2.1 lib/mocktail/matchers/captor.rb
mocktail-1.2.0 lib/mocktail/matchers/captor.rb
mocktail-1.1.3 lib/mocktail/matchers/captor.rb
mocktail-1.1.2 lib/mocktail/matchers/captor.rb
mocktail-1.1.1 lib/mocktail/matchers/captor.rb
mocktail-1.1.0 lib/mocktail/matchers/captor.rb
mocktail-1.0.0 lib/mocktail/matchers/captor.rb