module RSpec::Mocks::Syntax
@api private Provides methods for enabling and disabling the available syntaxes provided by rspec-mocks.
Public Class Methods
@api private Determines where the methods like `should_receive`, and `stub` are added.
# File lib/rspec/mocks/syntax.rb, line 168 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
@api private Disables the expect syntax (`expect(dbl).to receive`, `allow(dbl).to receive`, etc).
# File lib/rspec/mocks/syntax.rb, line 139 def self.disable_expect(syntax_host = ::RSpec::Mocks::ExampleMethods) return unless expect_enabled?(syntax_host) syntax_host.class_eval do undef receive undef allow undef expect_any_instance_of undef allow_any_instance_of end RSpec::Mocks::ExampleMethods::ExpectHost.class_eval do undef expect end end
@api private Disables the should syntax (`dbl.stub`, `dbl.should_receive`, etc).
# File lib/rspec/mocks/syntax.rb, line 86 def self.disable_should(syntax_host = default_should_syntax_host) return unless should_enabled?(syntax_host) syntax_host.class_eval do undef should_receive undef should_not_receive undef stub undef unstub undef stub! undef unstub! undef stub_chain undef as_null_object undef null_object? undef received_message? end Class.class_eval do undef any_instance end end
@api private Enables the expect syntax (`expect(dbl).to receive`, `allow(dbl).to receive`, etc).
# File lib/rspec/mocks/syntax.rb, line 109 def self.enable_expect(syntax_host = ::RSpec::Mocks::ExampleMethods) return if expect_enabled?(syntax_host) syntax_host.class_eval do def receive(method_name, &block) Matchers::Receive.new(method_name, 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_eval do def expect(target) ExpectationTarget.new(target) end end end
@api private Enables the should syntax (`dbl.stub`, `dbl.should_receive`, etc).
# File lib/rspec/mocks/syntax.rb, line 25 def self.enable_should(syntax_host = default_should_syntax_host) return if should_enabled?(syntax_host) syntax_host.class_eval do def should_receive(message, opts={}, &block) opts[:expected_from] ||= caller(1)[0] ::RSpec::Mocks.expect_message(self, message.to_sym, opts, &block) end def should_not_receive(message, &block) opts = {:expected_from => caller(1)[0]} ::RSpec::Mocks.expect_message(self, message.to_sym, opts, &block).never end def stub(message_or_hash, opts={}, &block) ::RSpec::Mocks::Syntax.stub_object(self, message_or_hash, opts, &block) end def unstub(message) ::RSpec::Mocks.space.proxy_for(self).remove_stub(message) end def stub!(message_or_hash, opts={}, &block) ::RSpec.deprecate "stub!", :replacement => "stub" ::RSpec::Mocks::Syntax.stub_object(self, message_or_hash, opts, &block) end def unstub!(message) ::RSpec.deprecate "unstub!", :replacement => "unstub" unstub(message) end def stub_chain(*chain, &blk) ::RSpec::Mocks::StubChain.stub_chain_on(self, *chain, &blk) end def as_null_object @_null_object = true ::RSpec::Mocks.proxy_for(self).as_null_object end def null_object? defined?(@_null_object) end def received_message?(message, *args, &block) ::RSpec::Mocks.proxy_for(self).received_message?(message, *args, &block) end unless Class.respond_to? :any_instance Class.class_eval do def any_instance ::RSpec::Mocks.any_instance_recorder_for(self) end end end end end
@api private Indicates whether or not the expect syntax is enabled.
# File lib/rspec/mocks/syntax.rb, line 162 def self.expect_enabled?(syntax_host = ::RSpec::Mocks::ExampleMethods) syntax_host.method_defined?(:allow) end
@api private Indicates whether or not the should syntax is enabled.
# File lib/rspec/mocks/syntax.rb, line 156 def self.should_enabled?(syntax_host = default_should_syntax_host) syntax_host.method_defined?(:should_receive) end
@api private
Common stubbing logic for both `stub` and `stub!`. This used to live in `stub`, and `stub!` delegated to `stub`, but we discovered that `stub!` was delegating to `RSpec::Mocks::ExampleMethods#stub` (which declares a test double) when called with an implicit receiver, which was a regression in 2.14.0.
# File lib/rspec/mocks/syntax.rb, line 14 def self.stub_object(object, message_or_hash, opts = {}, &block) if ::Hash === message_or_hash message_or_hash.each {|message, value| stub_object(object, message).and_return value } else opts[:expected_from] = caller(1)[1] ::RSpec::Mocks.allow_message(object, message_or_hash, opts, &block) end end
Public Instance Methods
# File lib/rspec/mocks/syntax.rb, line 117 def allow(target) AllowanceTarget.new(target) end
# File lib/rspec/mocks/syntax.rb, line 125 def allow_any_instance_of(klass) AnyInstanceAllowanceTarget.new(klass) end
# File lib/rspec/mocks/syntax.rb, line 76 def any_instance ::RSpec::Mocks.any_instance_recorder_for(self) end
# File lib/rspec/mocks/syntax.rb, line 61 def as_null_object @_null_object = true ::RSpec::Mocks.proxy_for(self).as_null_object end
# File lib/rspec/mocks/syntax.rb, line 131 def expect(target) ExpectationTarget.new(target) end
# File lib/rspec/mocks/syntax.rb, line 121 def expect_any_instance_of(klass) AnyInstanceExpectationTarget.new(klass) end
# File lib/rspec/mocks/syntax.rb, line 66 def null_object? defined?(@_null_object) end
# File lib/rspec/mocks/syntax.rb, line 113 def receive(method_name, &block) Matchers::Receive.new(method_name, block) end
# File lib/rspec/mocks/syntax.rb, line 70 def received_message?(message, *args, &block) ::RSpec::Mocks.proxy_for(self).received_message?(message, *args, &block) end
# File lib/rspec/mocks/syntax.rb, line 34 def should_not_receive(message, &block) opts = {:expected_from => caller(1)[0]} ::RSpec::Mocks.expect_message(self, message.to_sym, opts, &block).never end
# File lib/rspec/mocks/syntax.rb, line 29 def should_receive(message, opts={}, &block) opts[:expected_from] ||= caller(1)[0] ::RSpec::Mocks.expect_message(self, message.to_sym, opts, &block) end
# File lib/rspec/mocks/syntax.rb, line 39 def stub(message_or_hash, opts={}, &block) ::RSpec::Mocks::Syntax.stub_object(self, message_or_hash, opts, &block) end
# File lib/rspec/mocks/syntax.rb, line 47 def stub!(message_or_hash, opts={}, &block) ::RSpec.deprecate "stub!", :replacement => "stub" ::RSpec::Mocks::Syntax.stub_object(self, message_or_hash, opts, &block) end
# File lib/rspec/mocks/syntax.rb, line 57 def stub_chain(*chain, &blk) ::RSpec::Mocks::StubChain.stub_chain_on(self, *chain, &blk) end
# File lib/rspec/mocks/syntax.rb, line 43 def unstub(message) ::RSpec::Mocks.space.proxy_for(self).remove_stub(message) end
# File lib/rspec/mocks/syntax.rb, line 52 def unstub!(message) ::RSpec.deprecate "unstub!", :replacement => "unstub" unstub(message) end