Skip to content

Commit

Permalink
Merge pull request #411 from rspec/allow-mutex-mocks
Browse files Browse the repository at this point in the history
Allow mocking Mutex.new
  • Loading branch information
JonRowe committed Apr 26, 2020
2 parents 7692dbb + e902725 commit f38f784
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 1 deletion.
10 changes: 9 additions & 1 deletion lib/rspec/support/reentrant_mutex.rb
Expand Up @@ -43,7 +43,15 @@ 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", :if => defined?(::Mutex) do
expect(Mutex).to receive(:new)
::Mutex.new
end
end

0 comments on commit f38f784

Please sign in to comment.