module RSpec::Mocks::Syntax
def self.default_should_syntax_host
- Api: - private
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
def self.disable_expect(syntax_host=::RSpec::Mocks::ExampleMethods)
- Api: - private
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
def self.disable_should(syntax_host=default_should_syntax_host)
- Api: - private
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
def self.enable_expect(syntax_host=::RSpec::Mocks::ExampleMethods)
- Api: - private
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
def self.enable_should(syntax_host=default_should_syntax_host)
- Api: - private
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
def self.expect_enabled?(syntax_host=::RSpec::Mocks::ExampleMethods)
- Api: - private
def self.expect_enabled?(syntax_host=::RSpec::Mocks::ExampleMethods) syntax_host.method_defined?(:allow) end
def self.should_enabled?(syntax_host=default_should_syntax_host)
- Api: - private
def self.should_enabled?(syntax_host=default_should_syntax_host) syntax_host.method_defined?(:should_receive) end
def self.warn_about_should!
- Private: -
def self.warn_about_should! @warn_about_should = true end
def self.warn_unless_should_configured(method_name , replacement="the new `:expect` syntax or explicitly enable `:should`")
- Private: -
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
def allow(target)
def allow(target) AllowanceTarget.new(target) end
def allow_any_instance_of(klass)
def allow_any_instance_of(klass) AnyInstanceAllowanceTarget.new(klass) end
def any_instance
def any_instance ::RSpec::Mocks::Syntax.warn_unless_should_configured(__method__) ::RSpec::Mocks.space.any_instance_proxy_for(self) end
def as_null_object
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 expect(target)
def expect(target) ExpectationTarget.new(target) end
def expect_any_instance_of(klass)
def expect_any_instance_of(klass) AnyInstanceExpectationTarget.new(klass) end
def null_object?
def null_object? ::RSpec::Mocks::Syntax.warn_unless_should_configured(__method__) defined?(@_null_object) end
def receive(method_name, &block)
def receive(method_name, &block) Matchers::Receive.new(method_name, block) end
def receive_message_chain(*messages, &block)
def receive_message_chain(*messages, &block) Matchers::ReceiveMessageChain.new(messages, &block) end
def receive_messages(message_return_value_hash)
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 received_message?(message, *args, &block)
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
def should_not_receive(message, &block)
def should_not_receive(message, &block) ::RSpec::Mocks::Syntax.warn_unless_should_configured(__method__) ::RSpec::Mocks.expect_message(self, message, {}, &block).never end
def should_receive(message, opts={}, &block)
def should_receive(message, opts={}, &block) ::RSpec::Mocks::Syntax.warn_unless_should_configured(__method__) ::RSpec::Mocks.expect_message(self, message, opts, &block) end
def stub(message_or_hash, opts={}, &block)
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 stub_chain(*chain, &blk)
def stub_chain(*chain, &blk) ::RSpec::Mocks::Syntax.warn_unless_should_configured(__method__) ::RSpec::Mocks::StubChain.stub_chain_on(self, *chain, &blk) end
def unstub(message)
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