Sha256: 8f6b19ce51982ed5212878efd26dfb4dcc2ec3ee5e280b551b08813eaa4d4199

Contents?: true

Size: 1.3 KB

Versions: 6

Compression:

Stored size: 1.3 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
    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

6 entries across 6 versions & 1 rubygems

Version Path
mocktail-0.0.6 lib/mocktail/matchers/captor.rb
mocktail-0.0.5 lib/mocktail/matchers/captor.rb
mocktail-0.0.4 lib/mocktail/matchers/captor.rb
mocktail-0.0.3 lib/mocktail/matchers/captor.rb
mocktail-0.0.2 lib/mocktail/matchers/captor.rb
mocktail-0.0.1 lib/mocktail/matchers/captor.rb