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

Commit 1db709e

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

File tree

2 files changed

+12
-1
lines changed

2 files changed

+12
-1
lines changed

lib/rspec/support.rb

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

33
class Thread
44
attr_accessor :__rspec_local_data
5+
6+
class << self
7+
alias __current_for_rspec current
8+
end
59
end
610

711
module RSpec
@@ -96,7 +100,7 @@ def self.class_of(object)
96100

97101
# A single thread local variable so we don't excessively pollute that namespace.
98102
def self.thread_local_data
99-
Thread.current.__rspec_local_data ||= {}
103+
Thread.__current_for_rspec.__rspec_local_data ||= {}
100104
end
101105

102106
# @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)