Skip to content

Commit

Permalink
Allow mocking Mutex.new
Browse files Browse the repository at this point in the history
  • Loading branch information
JonRowe committed Apr 26, 2020
1 parent 5915149 commit a27d344
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 1 deletion.
11 changes: 10 additions & 1 deletion lib/rspec/support/reentrant_mutex.rb
Expand Up @@ -43,7 +43,16 @@ def exit

if defined? ::Mutex
# On 1.9 and up, this is in core, so we just use the real one
Mutex = ::Mutex
class Mutex < ::Mutex

# If you mock Mutex.new you break our usage of Mutex, so
# instead we capture the original method to return Mutexs.
NEW_MUTEX_METHOD = Mutex.method(:new)

def self.new
NEW_MUTEX_METHOD.call
end
end
else # For 1.8.7
# :nocov:
RSpec::Support.require_rspec_support "mutex"
Expand Down
8 changes: 8 additions & 0 deletions spec/rspec/support/mutex_spec.rb
@@ -0,0 +1,8 @@
require 'rspec/support/mutex'

RSpec.describe RSpec::Support::Mutex do
it "allows ::Mutex to be mocked" do
expect(Mutex).to receive(:new)
::Mutex.new
end
end

0 comments on commit a27d344

Please sign in to comment.