Module: RSpec::Mocks::Syntax

### Home Classes Methods

### In Files

- lib/rspec/mocks/syntax.rb

### Methods

- ::default_should_syntax_host
- ::disable_expect
- ::disable_should
- ::enable_expect
- ::enable_should
- ::expect_enabled?
- ::should_enabled?
- ::warn_about_should!
- ::warn_unless_should_configured
- #allow
- #allow_any_instance_of
- #any_instance
- #as_null_object
- #expect
- #expect_any_instance_of
- #null_object?
- #receive
- #receive_message_chain
- #receive_messages
- #received_message?
- #should_not_receive
- #should_receive
- #stub
- #stub_chain
- #unstub

### Class/Module Index

Quicksearch

- RSpec
- RSpec::Mocks
- RSpec::Mocks::AllowanceTarget
- RSpec::Mocks::AndReturnImplementation
- RSpec::Mocks::AndWrapOriginalImplementation
- RSpec::Mocks::AndYieldImplementation
- RSpec::Mocks::AnyInstance
- RSpec::Mocks::AnyInstance::Chain
- RSpec::Mocks::AnyInstance::Chain::Customizations
- RSpec::Mocks::AnyInstance::ErrorGenerator
- RSpec::Mocks::AnyInstance::ExpectChainChain
- RSpec::Mocks::AnyInstance::ExpectationChain
- RSpec::Mocks::AnyInstance::FluentInterfaceProxy
- RSpec::Mocks::AnyInstance::MessageChains
- RSpec::Mocks::AnyInstance::PositiveExpectationChain
- RSpec::Mocks::AnyInstance::Proxy
- RSpec::Mocks::AnyInstance::Recorder
- RSpec::Mocks::AnyInstance::StubChain
- RSpec::Mocks::AnyInstance::StubChainChain
- RSpec::Mocks::AnyInstanceAllowanceTarget
- RSpec::Mocks::AnyInstanceExpectationTarget
- RSpec::Mocks::ArgumentListMatcher
- RSpec::Mocks::ArgumentMatchers
- RSpec::Mocks::ArgumentMatchers::AnyArgMatcher
- RSpec::Mocks::ArgumentMatchers::AnyArgsMatcher
- RSpec::Mocks::ArgumentMatchers::ArrayIncludingMatcher
- RSpec::Mocks::ArgumentMatchers::BaseHashMatcher
- RSpec::Mocks::ArgumentMatchers::BooleanMatcher
- RSpec::Mocks::ArgumentMatchers::DuckTypeMatcher
- RSpec::Mocks::ArgumentMatchers::HashExcludingMatcher
- RSpec::Mocks::ArgumentMatchers::HashIncludingMatcher
- RSpec::Mocks::ArgumentMatchers::InstanceOf
- RSpec::Mocks::ArgumentMatchers::KindOf
- RSpec::Mocks::ArgumentMatchers::NoArgsMatcher
- RSpec::Mocks::ArgumentMatchers::SingletonMatcher
- RSpec::Mocks::CallbackInvocationStrategy
- RSpec::Mocks::ClassNewMethodReference
- RSpec::Mocks::ClassVerifyingDouble
- RSpec::Mocks::Configuration
- RSpec::Mocks::Constant
- RSpec::Mocks::ConstantMutator
- RSpec::Mocks::ConstantMutator::BaseMutator
- RSpec::Mocks::ConstantMutator::ConstantHider
- RSpec::Mocks::ConstantMutator::DefinedConstantReplacer
- RSpec::Mocks::ConstantMutator::UndefinedConstantSetter
- RSpec::Mocks::DirectObjectReference
- RSpec::Mocks::Double
- RSpec::Mocks::ErrorGenerator
- RSpec::Mocks::ExampleMethods
- RSpec::Mocks::ExampleMethods::ExpectHost
- RSpec::Mocks::ExpectChain
- RSpec::Mocks::ExpectationTarget
- RSpec::Mocks::Implementation
- RSpec::Mocks::InstanceMethodReference
- RSpec::Mocks::InstanceMethodStasher
- RSpec::Mocks::InstanceVerifyingDouble
- RSpec::Mocks::MarshalExtension
- RSpec::Mocks::Matchers
- RSpec::Mocks::Matchers::ExpectationCustomization
- RSpec::Mocks::Matchers::HaveReceived
- RSpec::Mocks::Matchers::Receive
- RSpec::Mocks::Matchers::Receive::DefaultDescribable
- RSpec::Mocks::Matchers::ReceiveMessageChain
- RSpec::Mocks::Matchers::ReceiveMessages
- RSpec::Mocks::MessageChain
- RSpec::Mocks::MessageExpectation
- RSpec::Mocks::MessageExpectation::ImplementationDetails
- RSpec::Mocks::MethodDouble
- RSpec::Mocks::MethodReference
- RSpec::Mocks::Mutex
- RSpec::Mocks::NamedObjectReference
- RSpec::Mocks::NestedSpace
- RSpec::Mocks::NoCallbackInvocationStrategy
- RSpec::Mocks::ObjectMethodReference
- RSpec::Mocks::ObjectReference
- RSpec::Mocks::ObjectVerifyingDouble
- RSpec::Mocks::ObjectVerifyingDoubleMethods
- RSpec::Mocks::OrderGroup
- RSpec::Mocks::PartialClassDoubleProxy
- RSpec::Mocks::PartialClassDoubleProxyMethods
- RSpec::Mocks::PartialDoubleProxy
- RSpec::Mocks::Proxy
- RSpec::Mocks::ProxyForNil
- RSpec::Mocks::ReentrantMutex
- RSpec::Mocks::RootSpace
- RSpec::Mocks::SimpleMessageExpectation
- RSpec::Mocks::Space
- RSpec::Mocks::StubChain
- RSpec::Mocks::Syntax
- RSpec::Mocks::TargetBase
- RSpec::Mocks::TestDouble
- RSpec::Mocks::TestDoubleFormatter
- RSpec::Mocks::TestDoubleProxy
- RSpec::Mocks::VerifyingDouble
- RSpec::Mocks::VerifyingDouble::SilentIO
- RSpec::Mocks::VerifyingExistingClassNewMethodDouble
- RSpec::Mocks::VerifyingExistingMethodDouble
- RSpec::Mocks::VerifyingMessageExpectation
- RSpec::Mocks::VerifyingMethodDouble
- RSpec::Mocks::VerifyingPartialClassDoubleProxy
- RSpec::Mocks::VerifyingPartialDoubleProxy
- RSpec::Mocks::VerifyingProxy
- RSpec::Mocks::VerifyingProxyMethods
- RSpec::Mocks::Version
- BasicObject
- Class

No matching classes.

# RSpec::Mocks::Syntax

@api private Provides methods for enabling and disabling the available syntaxes provided by rspec-mocks.

### Public Class Methods

default_should_syntax_host() click to toggle source

@api private Determines where the methods like `should_receive`, and `stub` are added.

    # File lib/rspec/mocks/syntax.rb, line 181
    def self.default_should_syntax_host
      # JRuby 1.7.4 introduces a regression whereby `defined?(::BasicObject) => nil`
      # yet `BasicObject` still exists and patching onto ::Object breaks things
      # e.g. SimpleDelegator expectations won't work
      #
      # See: https://github.com/jruby/jruby/issues/814
      if defined?(JRUBY_VERSION) && JRUBY_VERSION == '1.7.4' && RUBY_VERSION.to_f > 1.8
        return ::BasicObject
      end

      # On 1.8.7, Object.ancestors.last == Kernel but
      # things blow up if we include `RSpec::Mocks::Methods`
      # into Kernel...not sure why.
      return Object unless defined?(::BasicObject)

      # MacRuby has BasicObject but it's not the root class.
      return Object unless Object.ancestors.last == ::BasicObject

      ::BasicObject
    end

disable_expect(syntax_host=::RSpec::Mocks::ExampleMethods) click to toggle source

@api private Disables the expect syntax (`expect(dbl).to receive`, `allow(dbl).to receive`, etc).

    # File lib/rspec/mocks/syntax.rb, line 150
    def self.disable_expect(syntax_host=::RSpec::Mocks::ExampleMethods)
      return unless expect_enabled?(syntax_host)

      syntax_host.class_exec do
        undef receive
        undef receive_messages
        undef receive_message_chain
        undef allow
        undef expect_any_instance_of
        undef allow_any_instance_of
      end

      RSpec::Mocks::ExampleMethods::ExpectHost.class_exec do
        undef expect
      end
    end

disable_should(syntax_host=default_should_syntax_host) click to toggle source

@api private Disables the should syntax (`dbl.stub`, `dbl.should_receive`, etc).

    # File lib/rspec/mocks/syntax.rb, line 89
    def self.disable_should(syntax_host=default_should_syntax_host)
      return unless should_enabled?(syntax_host)

      syntax_host.class_exec do
        undef should_receive
        undef should_not_receive
        undef stub
        undef unstub
        undef stub_chain
        undef as_null_object
        undef null_object?
        undef received_message?
      end

      Class.class_exec do
        undef any_instance
      end
    end

enable_expect(syntax_host=::RSpec::Mocks::ExampleMethods) click to toggle source

@api private Enables the expect syntax (`expect(dbl).to receive`, `allow(dbl).to receive`, etc).

    # File lib/rspec/mocks/syntax.rb, line 110
    def self.enable_expect(syntax_host=::RSpec::Mocks::ExampleMethods)
      return if expect_enabled?(syntax_host)

      syntax_host.class_exec do
        def receive(method_name, &block)
          Matchers::Receive.new(method_name, block)
        end

        def receive_messages(message_return_value_hash)
          matcher = Matchers::ReceiveMessages.new(message_return_value_hash)
          matcher.warn_about_block if block_given?
          matcher
        end

        def receive_message_chain(*messages, &block)
          Matchers::ReceiveMessageChain.new(messages, &block)
        end

        def allow(target)
          AllowanceTarget.new(target)
        end

        def expect_any_instance_of(klass)
          AnyInstanceExpectationTarget.new(klass)
        end

        def allow_any_instance_of(klass)
          AnyInstanceAllowanceTarget.new(klass)
        end
      end

      RSpec::Mocks::ExampleMethods::ExpectHost.class_exec do
        def expect(target)
          ExpectationTarget.new(target)
        end
      end
    end

enable_should(syntax_host=default_should_syntax_host) click to toggle source

@api private Enables the should syntax (`dbl.stub`, `dbl.should_receive`, etc).

    # File lib/rspec/mocks/syntax.rb, line 26
    def self.enable_should(syntax_host=default_should_syntax_host)
      @warn_about_should = false if syntax_host == default_should_syntax_host
      return if should_enabled?(syntax_host)

      syntax_host.class_exec do
        def should_receive(message, opts={}, &block)
          ::RSpec::Mocks::Syntax.warn_unless_should_configured(__method__)
          ::RSpec::Mocks.expect_message(self, message, opts, &block)
        end

        def should_not_receive(message, &block)
          ::RSpec::Mocks::Syntax.warn_unless_should_configured(__method__)
          ::RSpec::Mocks.expect_message(self, message, {}, &block).never
        end

        def stub(message_or_hash, opts={}, &block)
          ::RSpec::Mocks::Syntax.warn_unless_should_configured(__method__)
          if ::Hash === message_or_hash
            message_or_hash.each { |message, value| stub(message).and_return value }
          else
            ::RSpec::Mocks.allow_message(self, message_or_hash, opts, &block)
          end
        end

        def unstub(message)
          ::RSpec::Mocks::Syntax.warn_unless_should_configured(__method__, "`allow(...).to_receive(...).and_call_original` or explicitly enable `:should`")
          ::RSpec::Mocks.space.proxy_for(self).remove_stub(message)
        end

        def stub_chain(*chain, &blk)
          ::RSpec::Mocks::Syntax.warn_unless_should_configured(__method__)
          ::RSpec::Mocks::StubChain.stub_chain_on(self, *chain, &blk)
        end

        def as_null_object
          ::RSpec::Mocks::Syntax.warn_unless_should_configured(__method__)
          @_null_object = true
          ::RSpec::Mocks.space.proxy_for(self).as_null_object
        end

        def null_object?
          ::RSpec::Mocks::Syntax.warn_unless_should_configured(__method__)
          defined?(@_null_object)
        end

        def received_message?(message, *args, &block)
          ::RSpec::Mocks::Syntax.warn_unless_should_configured(__method__)
          ::RSpec::Mocks.space.proxy_for(self).received_message?(message, *args, &block)
        end

        unless Class.respond_to? :any_instance
          Class.class_exec do
            def any_instance
              ::RSpec::Mocks::Syntax.warn_unless_should_configured(__method__)
              ::RSpec::Mocks.space.any_instance_proxy_for(self)
            end
          end
        end
      end
    end

expect_enabled?(syntax_host=::RSpec::Mocks::ExampleMethods) click to toggle source

@api private Indicates whether or not the expect syntax is enabled.

    # File lib/rspec/mocks/syntax.rb, line 175
    def self.expect_enabled?(syntax_host=::RSpec::Mocks::ExampleMethods)
      syntax_host.method_defined?(:allow)
    end

should_enabled?(syntax_host=default_should_syntax_host) click to toggle source

@api private Indicates whether or not the should syntax is enabled.

    # File lib/rspec/mocks/syntax.rb, line 169
    def self.should_enabled?(syntax_host=default_should_syntax_host)
      syntax_host.method_defined?(:should_receive)
    end

warn_about_should!() click to toggle source

@private

    # File lib/rspec/mocks/syntax.rb, line 8
    def self.warn_about_should!
      @warn_about_should = true
    end

warn_unless_should_configured(method_name , replacement="the new `:expect` syntax or explicitly enable `:should`") click to toggle source

@private

    # File lib/rspec/mocks/syntax.rb, line 13
    def self.warn_unless_should_configured(method_name , replacement="the new `:expect` syntax or explicitly enable `:should`")
      if @warn_about_should
        RSpec.deprecate(
          "Using `#{method_name}` from rspec-mocks' old `:should` syntax without explicitly enabling the syntax",
          :replacement => replacement
        )

        @warn_about_should = false
      end
    end

### Public Instance Methods

allow(target) click to toggle source

    # File lib/rspec/mocks/syntax.rb, line 128
    def allow(target)
      AllowanceTarget.new(target)
    end

allow_any_instance_of(klass) click to toggle source

    # File lib/rspec/mocks/syntax.rb, line 136
    def allow_any_instance_of(klass)
      AnyInstanceAllowanceTarget.new(klass)
    end

any_instance() click to toggle source

    # File lib/rspec/mocks/syntax.rb, line 78
    def any_instance
      ::RSpec::Mocks::Syntax.warn_unless_should_configured(__method__)
      ::RSpec::Mocks.space.any_instance_proxy_for(self)
    end

as_null_object() click to toggle source

    # File lib/rspec/mocks/syntax.rb, line 60
    def as_null_object
      ::RSpec::Mocks::Syntax.warn_unless_should_configured(__method__)
      @_null_object = true
      ::RSpec::Mocks.space.proxy_for(self).as_null_object
    end

expect(target) click to toggle source

    # File lib/rspec/mocks/syntax.rb, line 142
    def expect(target)
      ExpectationTarget.new(target)
    end

expect_any_instance_of(klass) click to toggle source

    # File lib/rspec/mocks/syntax.rb, line 132
    def expect_any_instance_of(klass)
      AnyInstanceExpectationTarget.new(klass)
    end

null_object?() click to toggle source

    # File lib/rspec/mocks/syntax.rb, line 66
    def null_object?
      ::RSpec::Mocks::Syntax.warn_unless_should_configured(__method__)
      defined?(@_null_object)
    end

receive(method_name, &block) click to toggle source

    # File lib/rspec/mocks/syntax.rb, line 114
    def receive(method_name, &block)
      Matchers::Receive.new(method_name, block)
    end

receive_message_chain(*messages, &block) click to toggle source

    # File lib/rspec/mocks/syntax.rb, line 124
    def receive_message_chain(*messages, &block)
      Matchers::ReceiveMessageChain.new(messages, &block)
    end

receive_messages(message_return_value_hash) click to toggle source

    # File lib/rspec/mocks/syntax.rb, line 118
    def receive_messages(message_return_value_hash)
      matcher = Matchers::ReceiveMessages.new(message_return_value_hash)
      matcher.warn_about_block if block_given?
      matcher
    end

received_message?(message, *args, &block) click to toggle source

    # File lib/rspec/mocks/syntax.rb, line 71
    def received_message?(message, *args, &block)
      ::RSpec::Mocks::Syntax.warn_unless_should_configured(__method__)
      ::RSpec::Mocks.space.proxy_for(self).received_message?(message, *args, &block)
    end

should_not_receive(message, &block) click to toggle source

    # File lib/rspec/mocks/syntax.rb, line 36
    def should_not_receive(message, &block)
      ::RSpec::Mocks::Syntax.warn_unless_should_configured(__method__)
      ::RSpec::Mocks.expect_message(self, message, {}, &block).never
    end

should_receive(message, opts={}, &block) click to toggle source

    # File lib/rspec/mocks/syntax.rb, line 31
    def should_receive(message, opts={}, &block)
      ::RSpec::Mocks::Syntax.warn_unless_should_configured(__method__)
      ::RSpec::Mocks.expect_message(self, message, opts, &block)
    end

stub(message_or_hash, opts={}, &block) click to toggle source

    # File lib/rspec/mocks/syntax.rb, line 41
    def stub(message_or_hash, opts={}, &block)
      ::RSpec::Mocks::Syntax.warn_unless_should_configured(__method__)
      if ::Hash === message_or_hash
        message_or_hash.each { |message, value| stub(message).and_return value }
      else
        ::RSpec::Mocks.allow_message(self, message_or_hash, opts, &block)
      end
    end

stub_chain(*chain, &blk) click to toggle source

    # File lib/rspec/mocks/syntax.rb, line 55
    def stub_chain(*chain, &blk)
      ::RSpec::Mocks::Syntax.warn_unless_should_configured(__method__)
      ::RSpec::Mocks::StubChain.stub_chain_on(self, *chain, &blk)
    end

unstub(message) click to toggle source

    # File lib/rspec/mocks/syntax.rb, line 50
    def unstub(message)
      ::RSpec::Mocks::Syntax.warn_unless_should_configured(__method__, "`allow(...).to_receive(...).and_call_original` or explicitly enable `:should`")
      ::RSpec::Mocks.space.proxy_for(self).remove_stub(message)
    end

[Validate]

Generated with the Darkfish Rdoc Generator 2.