Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow mocking Mutex.new #411

Merged
merged 1 commit into from Apr 26, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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