diff --git a/lib/rspec/mocks/proxy.rb b/lib/rspec/mocks/proxy.rb index 6e0268166..45beb5515 100644 --- a/lib/rspec/mocks/proxy.rb +++ b/lib/rspec/mocks/proxy.rb @@ -1,3 +1,5 @@ +RSpec::Support.require_rspec_support 'mutex' + module RSpec module Mocks # @private @@ -9,11 +11,6 @@ def ==(expectation) end end - unless defined?(Mutex) - Support.require_rspec_support 'mutex' - Mutex = Support::Mutex - end - # @private def ensure_implemented(*_args) # noop for basic proxies, see VerifyingProxy for behaviour. @@ -27,7 +24,7 @@ def initialize(object, order_group, options={}) @order_group = order_group @error_generator = ErrorGenerator.new(object) @messages_received = [] - @messages_received_mutex = Mutex.new + @messages_received_mutex = Support::Mutex.new @options = options @null_object = false @method_doubles = Hash.new { |h, k| h[k] = MethodDouble.new(@object, k, self) } diff --git a/spec/rspec/mocks/mutex_spec.rb b/spec/rspec/mocks/mutex_spec.rb new file mode 100644 index 000000000..9b163fff7 --- /dev/null +++ b/spec/rspec/mocks/mutex_spec.rb @@ -0,0 +1,18 @@ +module RSpec + module Mocks + RSpec.describe "Mocking Mutex" do + let(:mocked_mutex) { instance_double(Mutex) } + before do + allow(Mutex).to receive(:new).and_return(mocked_mutex) + allow(mocked_mutex).to receive(:synchronize).and_yield + end + + it "successfully yields" do + called = false + mutex = Mutex.new + mutex.synchronize { called = true } + expect(called).to be_truthy + end + end + end +end