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

Commit d5745fb

Browse files
committed
Protect against mocking Thread.current
1 parent 1a69d22 commit d5745fb

File tree

2 files changed

+11
-1
lines changed

2 files changed

+11
-1
lines changed

lib/rspec/support.rb

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,9 @@
22

33
class Thread
44
attr_accessor :__rspec_local_data
5+
class << self
6+
alias __current_for_rspec current
7+
end
58
end
69

710
module RSpec
@@ -96,7 +99,7 @@ def self.class_of(object)
9699

97100
# A single thread local variable so we don't excessively pollute that namespace.
98101
def self.thread_local_data
99-
Thread.current.__rspec_local_data ||= {}
102+
Thread.__current_for_rspec.__rspec_local_data ||= {}
100103
end
101104

102105
# @api private

spec/rspec/support_spec.rb

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -205,6 +205,13 @@ def object.some_method
205205
end
206206
end
207207

208+
it "works when Thread.current is mocked" do
209+
expect(Thread).to_not receive(:current)
210+
211+
RSpec::Support.thread_local_data[:__for_test] = :oh_hai
212+
expect(RSpec::Support.thread_local_data[:__for_test]).to eq :oh_hai
213+
end
214+
208215
it "works when Thread#thread_variable_get and Thread#thread_variable_set are mocked" do
209216
expect(Thread.current).to receive(:thread_variable_set).with(:test, true).once.and_return(true)
210217
expect(Thread.current).to receive(:thread_variable_get).with(:test).once.and_return(true)

0 commit comments

Comments
 (0)