Sha256: 96e565e4765b1b46b586b36a9e88c7504333a8e5431faacc9c0c8b93ea0ff622

Contents?: true

Size: 1.94 KB

Versions: 12

Compression:

Stored size: 1.94 KB

Contents

module RSpec
  module Mocks
    module AnyInstance
      class Chain
        class << self
          private

          # @macro [attach] record
          #   @method $1(*args, &block)
          #   Records the `$1` message for playback against an instance that
          #   invokes a method stubbed or mocked using `any_instance`.
          #
          #   @see RSpec::Mocks::MessageExpectation#$1
          #
          def record(method_name)
            class_eval(<<-EOM, __FILE__, __LINE__ + 1)
              def #{method_name}(*args, &block)
                record(:#{method_name}, *args, &block)
              end
            EOM
          end
        end

        record :and_return
        record :and_raise
        record :and_throw
        record :and_yield
        record :and_call_original
        record :with
        record :once
        record :twice
        record :any_number_of_times
        record :exactly
        record :times
        record :never
        record :at_least
        record :at_most

        # @private
        def playback!(instance)
          messages.inject(instance) do |_instance, message|
            _instance.__send__(*message.first, &message.last)
          end
        end

        # @private
        def constrained_to_any_of?(*constraints)
          constraints.any? do |constraint|
            messages.any? do |message|
              message.first.first == constraint
            end
          end
        end

        # @private
        def expectation_fulfilled!
          @expectation_fulfilled = true
        end

        private

        def messages
          @messages ||= []
        end

        def last_message
          messages.last.first.first unless messages.empty?
        end

        def record(rspec_method_name, *args, &block)
          verify_invocation_order(rspec_method_name, *args, &block)
          messages << [args.unshift(rspec_method_name), block]
          self
        end
      end
    end
  end
end

Version data entries

12 entries across 12 versions & 6 rubygems

Version Path
judge-2.0.5 vendor/bundle/ruby/2.1.0/gems/rspec-mocks-2.13.0/lib/rspec/mocks/any_instance/chain.rb
sshp-0.0.2 vendor/ruby/1.9.1/gems/rspec-mocks-2.13.1/lib/rspec/mocks/any_instance/chain.rb
sshp-0.0.1 vendor/ruby/1.9.1/gems/rspec-mocks-2.13.1/lib/rspec/mocks/any_instance/chain.rb
sidekiq-statsd-0.1.1 vendor/ruby/1.9.1/gems/rspec-mocks-2.13.1/lib/rspec/mocks/any_instance/chain.rb
sidekiq-statsd-0.1.0 vendor/ruby/1.9.1/gems/rspec-mocks-2.13.1/lib/rspec/mocks/any_instance/chain.rb
rspec-mocks-2.13.1 lib/rspec/mocks/any_instance/chain.rb
vagrant-actionio-0.0.9 vendor/bundle/gems/rspec-mocks-2.13.0/lib/rspec/mocks/any_instance/chain.rb
rspec-mocks-2.13.0 lib/rspec/mocks/any_instance/chain.rb
remq-0.0.4 vendor/bundle/gems/rspec-mocks-2.12.2/lib/rspec/mocks/any_instance/chain.rb
remq-0.0.3 vendor/bundle/gems/rspec-mocks-2.12.2/lib/rspec/mocks/any_instance/chain.rb
rspec-mocks-2.12.2 lib/rspec/mocks/any_instance/chain.rb
rspec-mocks-2.12.1 lib/rspec/mocks/any_instance/chain.rb