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

Commit a27d344

Browse files
committed
Allow mocking Mutex.new
1 parent 5915149 commit a27d344

File tree

2 files changed

+18
-1
lines changed

2 files changed

+18
-1
lines changed

lib/rspec/support/reentrant_mutex.rb

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,16 @@ def exit
4343

4444
if defined? ::Mutex
4545
# On 1.9 and up, this is in core, so we just use the real one
46-
Mutex = ::Mutex
46+
class Mutex < ::Mutex
47+
48+
# If you mock Mutex.new you break our usage of Mutex, so
49+
# instead we capture the original method to return Mutexs.
50+
NEW_MUTEX_METHOD = Mutex.method(:new)
51+
52+
def self.new
53+
NEW_MUTEX_METHOD.call
54+
end
55+
end
4756
else # For 1.8.7
4857
# :nocov:
4958
RSpec::Support.require_rspec_support "mutex"

spec/rspec/support/mutex_spec.rb

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
require 'rspec/support/mutex'
2+
3+
RSpec.describe RSpec::Support::Mutex do
4+
it "allows ::Mutex to be mocked" do
5+
expect(Mutex).to receive(:new)
6+
::Mutex.new
7+
end
8+
end

0 commit comments

Comments
 (0)