Skip to content
This repository was archived by the owner on Nov 30, 2024. It is now read-only.

Commit c08195f

Browse files
committed
Relax, BasicObject is defined
1 parent bb8636e commit c08195f

File tree

4 files changed

+96
-107
lines changed

4 files changed

+96
-107
lines changed

lib/rspec/mocks/syntax.rb

Lines changed: 92 additions & 103 deletions
Original file line numberDiff line numberDiff line change
@@ -23,8 +23,8 @@ def self.warn_unless_should_configured(method_name , replacement="the new `:expe
2323

2424
# @api private
2525
# Enables the should syntax (`dbl.stub`, `dbl.should_receive`, etc).
26-
def self.enable_should(syntax_host=default_should_syntax_host)
27-
@warn_about_should = false if syntax_host == default_should_syntax_host
26+
def self.enable_should(syntax_host=::BasicObject)
27+
@warn_about_should = false if syntax_host == ::BasicObject
2828
return if should_enabled?(syntax_host)
2929

3030
syntax_host.class_exec do
@@ -86,7 +86,7 @@ def any_instance
8686

8787
# @api private
8888
# Disables the should syntax (`dbl.stub`, `dbl.should_receive`, etc).
89-
def self.disable_should(syntax_host=default_should_syntax_host)
89+
def self.disable_should(syntax_host=::BasicObject)
9090
return unless should_enabled?(syntax_host)
9191

9292
syntax_host.class_exec do
@@ -166,7 +166,7 @@ def self.disable_expect(syntax_host=::RSpec::Mocks::ExampleMethods)
166166

167167
# @api private
168168
# Indicates whether or not the should syntax is enabled.
169-
def self.should_enabled?(syntax_host=default_should_syntax_host)
169+
def self.should_enabled?(syntax_host=::BasicObject)
170170
syntax_host.method_defined?(:should_receive)
171171
end
172172

@@ -175,109 +175,98 @@ def self.should_enabled?(syntax_host=default_should_syntax_host)
175175
def self.expect_enabled?(syntax_host=::RSpec::Mocks::ExampleMethods)
176176
syntax_host.method_defined?(:allow)
177177
end
178-
179-
# @api private
180-
# Determines where the methods like `should_receive`, and `stub` are added.
181-
def self.default_should_syntax_host
182-
# MacRuby has BasicObject but it's not the root class.
183-
return Object unless Object.ancestors.last == ::BasicObject
184-
185-
::BasicObject
186-
end
187178
end
188179
end
189180
end
190181

191-
if defined?(BasicObject)
192-
# The legacy `:should` syntax adds the following methods directly to
193-
# `BasicObject` so that they are available off of any object. Note, however,
194-
# that this syntax does not always play nice with delegate/proxy objects.
195-
# We recommend you use the non-monkeypatching `:expect` syntax instead.
196-
# @see Class
197-
class BasicObject
198-
# @method should_receive
199-
# Sets an expectation that this object should receive a message before
200-
# the end of the example.
201-
#
202-
# @example
203-
# logger = double('logger')
204-
# thing_that_logs = ThingThatLogs.new(logger)
205-
# logger.should_receive(:log)
206-
# thing_that_logs.do_something_that_logs_a_message
207-
#
208-
# @note This is only available when you have enabled the `should` syntax.
209-
# @see RSpec::Mocks::ExampleMethods#expect
210-
211-
# @method should_not_receive
212-
# Sets and expectation that this object should _not_ receive a message
213-
# during this example.
214-
# @see RSpec::Mocks::ExampleMethods#expect
215-
216-
# @method stub
217-
# Tells the object to respond to the message with the specified value.
218-
#
219-
# @example
220-
# counter.stub(:count).and_return(37)
221-
# counter.stub(:count => 37)
222-
# counter.stub(:count) { 37 }
223-
#
224-
# @note This is only available when you have enabled the `should` syntax.
225-
# @see RSpec::Mocks::ExampleMethods#allow
226-
227-
# @method unstub
228-
# Removes a stub. On a double, the object will no longer respond to
229-
# `message`. On a real object, the original method (if it exists) is
230-
# restored.
231-
#
232-
# This is rarely used, but can be useful when a stub is set up during a
233-
# shared `before` hook for the common case, but you want to replace it
234-
# for a special case.
235-
#
236-
# @note This is only available when you have enabled the `should` syntax.
237-
238-
# @method stub_chain
239-
# @overload stub_chain(method1, method2)
240-
# @overload stub_chain("method1.method2")
241-
# @overload stub_chain(method1, method_to_value_hash)
242-
#
243-
# Stubs a chain of methods.
244-
#
245-
# ## Warning:
246-
#
247-
# Chains can be arbitrarily long, which makes it quite painless to
248-
# violate the Law of Demeter in violent ways, so you should consider any
249-
# use of `stub_chain` a code smell. Even though not all code smells
250-
# indicate real problems (think fluent interfaces), `stub_chain` still
251-
# results in brittle examples. For example, if you write
252-
# `foo.stub_chain(:bar, :baz => 37)` in a spec and then the
253-
# implementation calls `foo.baz.bar`, the stub will not work.
254-
#
255-
# @example
256-
# double.stub_chain("foo.bar") { :baz }
257-
# double.stub_chain(:foo, :bar => :baz)
258-
# double.stub_chain(:foo, :bar) { :baz }
259-
#
260-
# # Given any of ^^ these three forms ^^:
261-
# double.foo.bar # => :baz
262-
#
263-
# # Common use in Rails/ActiveRecord:
264-
# Article.stub_chain("recent.published") { [Article.new] }
265-
#
266-
# @note This is only available when you have enabled the `should` syntax.
267-
# @see RSpec::Mocks::ExampleMethods#receive_message_chain
268-
269-
# @method as_null_object
270-
# Tells the object to respond to all messages. If specific stub values
271-
# are declared, they'll work as expected. If not, the receiver is
272-
# returned.
273-
#
274-
# @note This is only available when you have enabled the `should` syntax.
275-
276-
# @method null_object?
277-
# Returns true if this object has received `as_null_object`
278-
#
279-
# @note This is only available when you have enabled the `should` syntax.
280-
end
182+
# The legacy `:should` syntax adds the following methods directly to
183+
# `BasicObject` so that they are available off of any object. Note, however,
184+
# that this syntax does not always play nice with delegate/proxy objects.
185+
# We recommend you use the non-monkeypatching `:expect` syntax instead.
186+
# @see Class
187+
class BasicObject
188+
# @method should_receive
189+
# Sets an expectation that this object should receive a message before
190+
# the end of the example.
191+
#
192+
# @example
193+
# logger = double('logger')
194+
# thing_that_logs = ThingThatLogs.new(logger)
195+
# logger.should_receive(:log)
196+
# thing_that_logs.do_something_that_logs_a_message
197+
#
198+
# @note This is only available when you have enabled the `should` syntax.
199+
# @see RSpec::Mocks::ExampleMethods#expect
200+
201+
# @method should_not_receive
202+
# Sets and expectation that this object should _not_ receive a message
203+
# during this example.
204+
# @see RSpec::Mocks::ExampleMethods#expect
205+
206+
# @method stub
207+
# Tells the object to respond to the message with the specified value.
208+
#
209+
# @example
210+
# counter.stub(:count).and_return(37)
211+
# counter.stub(:count => 37)
212+
# counter.stub(:count) { 37 }
213+
#
214+
# @note This is only available when you have enabled the `should` syntax.
215+
# @see RSpec::Mocks::ExampleMethods#allow
216+
217+
# @method unstub
218+
# Removes a stub. On a double, the object will no longer respond to
219+
# `message`. On a real object, the original method (if it exists) is
220+
# restored.
221+
#
222+
# This is rarely used, but can be useful when a stub is set up during a
223+
# shared `before` hook for the common case, but you want to replace it
224+
# for a special case.
225+
#
226+
# @note This is only available when you have enabled the `should` syntax.
227+
228+
# @method stub_chain
229+
# @overload stub_chain(method1, method2)
230+
# @overload stub_chain("method1.method2")
231+
# @overload stub_chain(method1, method_to_value_hash)
232+
#
233+
# Stubs a chain of methods.
234+
#
235+
# ## Warning:
236+
#
237+
# Chains can be arbitrarily long, which makes it quite painless to
238+
# violate the Law of Demeter in violent ways, so you should consider any
239+
# use of `stub_chain` a code smell. Even though not all code smells
240+
# indicate real problems (think fluent interfaces), `stub_chain` still
241+
# results in brittle examples. For example, if you write
242+
# `foo.stub_chain(:bar, :baz => 37)` in a spec and then the
243+
# implementation calls `foo.baz.bar`, the stub will not work.
244+
#
245+
# @example
246+
# double.stub_chain("foo.bar") { :baz }
247+
# double.stub_chain(:foo, :bar => :baz)
248+
# double.stub_chain(:foo, :bar) { :baz }
249+
#
250+
# # Given any of ^^ these three forms ^^:
251+
# double.foo.bar # => :baz
252+
#
253+
# # Common use in Rails/ActiveRecord:
254+
# Article.stub_chain("recent.published") { [Article.new] }
255+
#
256+
# @note This is only available when you have enabled the `should` syntax.
257+
# @see RSpec::Mocks::ExampleMethods#receive_message_chain
258+
259+
# @method as_null_object
260+
# Tells the object to respond to all messages. If specific stub values
261+
# are declared, they'll work as expected. If not, the receiver is
262+
# returned.
263+
#
264+
# @note This is only available when you have enabled the `should` syntax.
265+
266+
# @method null_object?
267+
# Returns true if this object has received `as_null_object`
268+
#
269+
# @note This is only available when you have enabled the `should` syntax.
281270
end
282271

283272
# The legacy `:should` syntax adds the `any_instance` to `Class`.

spec/rspec/mocks/any_instance_spec.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -770,7 +770,7 @@ def inspect
770770
end
771771
end
772772

773-
it 'works with a BasicObject subclass that mixes in Kernel', :if => defined?(BasicObject) do
773+
it 'works with a BasicObject subclass that mixes in Kernel' do
774774
klazz = Class.new(BasicObject) do
775775
include ::Kernel
776776
def foo; end

spec/rspec/mocks/configuration_spec.rb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@ def sandboxed
6464
end
6565

6666
it 'is a no-op when configured a second time' do
67-
expect(Syntax.default_should_syntax_host).not_to receive(:method_undefined)
67+
expect(::BasicObject).not_to receive(:method_undefined)
6868
expect(::RSpec::Mocks::ExampleMethods).not_to receive(:method_added)
6969
configure_syntax :expect
7070
end
@@ -95,7 +95,7 @@ def sandboxed
9595
end
9696

9797
it 'is a no-op when configured a second time' do
98-
Syntax.default_should_syntax_host.should_not_receive(:method_added)
98+
::BasicObject.should_not_receive(:method_added)
9999
::RSpec::Mocks::ExampleMethods.should_not_receive(:method_undefined)
100100
configure_syntax :should
101101
end

spec/rspec/mocks/stub_spec.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -115,7 +115,7 @@ def existing_private_instance_method
115115

116116
context "when the stubbed method is called" do
117117
it "does not call any methods on the passed args, since that could mutate them", :issue => 892 do
118-
recorder = Class.new(defined?(::BasicObject) ? ::BasicObject : ::Object) do
118+
recorder = Class.new(::BasicObject) do
119119
def called_methods
120120
@called_methods ||= []
121121
end

0 commit comments

Comments
 (0)