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

Commit c699dec

Browse files
committed
Stash methods that get and set thread local variables to allow the user to mock them
Fixes #605.
1 parent 42b18c4 commit c699dec

File tree

1 file changed

+10
-7
lines changed

1 file changed

+10
-7
lines changed

lib/rspec/support.rb

+10-7
Original file line numberDiff line numberDiff line change
@@ -90,15 +90,18 @@ def self.class_of(object)
9090
singleton_class.ancestors.find { |ancestor| !ancestor.equal?(singleton_class) }
9191
end
9292

93-
# A single thread local variable so we don't excessively pollute that namespace.
93+
# Stash original methods to allow the user to mock them.
9494
if RUBY_VERSION.to_f >= 2
95-
def self.thread_local_data
96-
Thread.current.thread_variable_get(:__rspec) || Thread.current.thread_variable_set(:__rspec, {})
97-
end
95+
@thread_variable_get = Thread.instance_method(:thread_variable_get)
96+
@thread_variable_set = Thread.instance_method(:thread_variable_set)
9897
else
99-
def self.thread_local_data
100-
Thread.current[:__rspec] ||= {}
101-
end
98+
@thread_variable_get = Thread.instance_method(:[])
99+
@thread_variable_set = Thread.instance_method(:[]=)
100+
end
101+
102+
# A single thread local variable so we don't excessively pollute that namespace.
103+
def self.thread_local_data
104+
@thread_variable_get.bind(Thread.current).call(:__rspec) || @thread_variable_set.bind(Thread.current).call(:__rspec, {})
102105
end
103106

104107
# @api private

0 commit comments

Comments
 (0)