Sha256: f34f870f8076c30e8b891aa7df345909c2762d4c6989a4fea3945d4d36a5f04d

Contents?: true

Size: 1.76 KB

Versions: 5

Compression:

Stored size: 1.76 KB

Contents

module Spy
  module API
    DidNotReceiveError = Class.new(Spy::Error)

    def assert_received(base_object, method_name)
      assert Subroutine.get(base_object, method_name).has_been_called?,
        "#{method_name} was not called on #{base_object.inspect}"
    end

    def assert_received_with(base_object, method_name, *args, &block)
      assert Subroutine.get(base_object, method_name).has_been_called_with?(*args, &block),
        "#{method_name} was not called on #{base_object.inspect} with #{args.inspect}"
    end

    def have_received(method_name)
      HaveReceived.new(method_name)
    end

    class HaveReceived
      attr_reader :method_name, :actual

      def initialize(method_name)
        @method_name = method_name
        @with = nil
      end

      def matches?(actual)
        @actual = actual
        case @with
        when Proc
          spy.has_been_called_with?(&@with)
        when Array
          spy.has_been_called_with?(*@with)
        else
          spy.has_been_called?
        end
      end

      def with(*args, &block)
        @with = block_given? ? block : args
        self
      end

      def failure_message_for_should
        "expected #{actual.inspect} to have received #{method_name.inspect}#{args_message}"
      end

      def failure_message_for_should_not
        "expected #{actual.inspect} to not have received #{method_name.inspect}#{args_message}, but did"
      end

      def description
        "to have received #{method_name.inspect}#{args_message}"
      end

      private

      def args_message
        case @with
        when Array
          " with #{@with.inspect}"
        when Proc
          " with given block"
        end
      end

      def spy
        @spy ||= Subroutine.get(@actual, @method_name)
      end
    end
  end
end

Version data entries

5 entries across 5 versions & 1 rubygems

Version Path
spy-1.0.5 lib/spy/api.rb
spy-1.0.4 lib/spy/api.rb
spy-1.0.3 lib/spy/api.rb
spy-1.0.2 lib/spy/api.rb
spy-1.0.1 lib/spy/api.rb