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

Commit

Permalink
Merge pull request #1357 from zhisme/main
Browse files Browse the repository at this point in the history
raise ArgumentError if object cannot be proxied
  • Loading branch information
JonRowe authored Dec 12, 2020
2 parents 0acbd99 + c934c3e commit 2d12304
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 0 deletions.
16 changes: 16 additions & 0 deletions lib/rspec/mocks/proxy.rb
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@ def ensure_implemented(*_args)

# @private
def initialize(object, order_group, options={})
ensure_can_be_proxied!(object)

@object = object
@order_group = order_group
@error_generator = ErrorGenerator.new(object)
Expand All @@ -31,6 +33,20 @@ def initialize(object, order_group, options={})
@method_doubles = Hash.new { |h, k| h[k] = MethodDouble.new(@object, k, self) }
end

# @private
def ensure_can_be_proxied!(object)
return unless object.is_a?(Symbol) || object.frozen?
return if object.nil?

msg = "Cannot proxy frozen objects"
if Symbol === object
msg << ". Symbols such as #{object} cannot be mocked or stubbed."
else
msg << ", rspec-mocks relies on proxies for method stubbing and expectations."
end
raise ArgumentError, msg
end

# @private
attr_reader :object

Expand Down
16 changes: 16 additions & 0 deletions spec/rspec/mocks/space_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -232,6 +232,22 @@ def create_generations
}.to raise_error(RSpec::Expectations::ExpectationNotMetError, /Diff/)
end

it 'raises ArgumentError with message if object is symbol' do
space1 = Space.new
object = :subject
expected_message = "Cannot proxy frozen objects. Symbols such as #{object} cannot be mocked or stubbed."

expect { space1.proxy_for(object) }.to raise_error(ArgumentError, expected_message)
end

it 'raises ArgumentError with message if object is frozen' do
space1 = Space.new
object = "subject".freeze
expected_message = "Cannot proxy frozen objects, rspec-mocks relies on proxies for method stubbing and expectations."

expect { space1.proxy_for(object) }.to raise_error(ArgumentError, expected_message)
end

def in_new_space_scope
RSpec::Mocks.setup
yield
Expand Down
12 changes: 12 additions & 0 deletions spec/rspec/mocks/syntax_agnostic_message_matchers_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,12 @@ module Mocks

reset subject
end

context 'when target cannot be proxied' do
it 'raises ArgumentError with message' do
expect { ::RSpec::Mocks.allow_message(:subject, :foo) { :a } }.to raise_error(ArgumentError)
end
end
end

RSpec.describe ".expect_message" do
Expand Down Expand Up @@ -91,6 +97,12 @@ module Mocks

verify subject
end

context 'when target cannot be proxied' do
it 'raises ArgumentError with message' do
expect { ::RSpec::Mocks.expect_message(:subject, :foo) { :a } }.to raise_error(ArgumentError)
end
end
end
end
end

0 comments on commit 2d12304

Please sign in to comment.