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

Commit f38f784

Browse files
authored
Merge pull request #411 from rspec/allow-mutex-mocks
Allow mocking Mutex.new
2 parents 7692dbb + e902725 commit f38f784

File tree

2 files changed

+17
-1
lines changed

2 files changed

+17
-1
lines changed

lib/rspec/support/reentrant_mutex.rb

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,15 @@ 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+
# If you mock Mutex.new you break our usage of Mutex, so
48+
# instead we capture the original method to return Mutexs.
49+
NEW_MUTEX_METHOD = Mutex.method(:new)
50+
51+
def self.new
52+
NEW_MUTEX_METHOD.call
53+
end
54+
end
4755
else # For 1.8.7
4856
# :nocov:
4957
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", :if => defined?(::Mutex) do
5+
expect(Mutex).to receive(:new)
6+
::Mutex.new
7+
end
8+
end

0 commit comments

Comments
 (0)