failed to mock :new if method(*) is overridden
Created by: proby
While attempting to upgrade rspec
in some very, very old code I discovered this interesting edge case bug. I've reduced it to the following case:
class TheExample
def self.method(*)
'tsk tsk tsk'
end
end
RSpec.configure do |config|
config.mock_with :rspec do |mocks|
mocks.verify_partial_doubles = true
end
end
RSpec.describe TheExample do
it 'shows the bug' do
allow(TheExample).to receive(:new)
end
end
Running rspec
(with -b
) over this file results in the following error:
F
Failures:
1) TheExample shows the bug
Failure/Error: allow(TheExample).to receive(:new)
NoMethodError:
undefined method `owner' for "tsk tsk tsk":String
# /Users/proby/.gem/ruby/2.1.2/gems/rspec-mocks-3.5.0/lib/rspec/mocks/method_reference.rb:192:in `applies_to?'
# /Users/proby/.gem/ruby/2.1.2/gems/rspec-mocks-3.5.0/lib/rspec/mocks/verifying_proxy.rb:194:in `for'
# /Users/proby/.gem/ruby/2.1.2/gems/rspec-mocks-3.5.0/lib/rspec/mocks/verifying_proxy.rb:117:in `block in initialize'
# /Users/proby/.gem/ruby/2.1.2/gems/rspec-mocks-3.5.0/lib/rspec/mocks/verifying_proxy.rb:39:in `yield'
# /Users/proby/.gem/ruby/2.1.2/gems/rspec-mocks-3.5.0/lib/rspec/mocks/verifying_proxy.rb:39:in `ensure_implemented'
# /Users/proby/.gem/ruby/2.1.2/gems/rspec-mocks-3.5.0/lib/rspec/mocks/verifying_proxy.rb:24:in `add_stub'
# /Users/proby/.gem/ruby/2.1.2/gems/rspec-mocks-3.5.0/lib/rspec/mocks/matchers/receive.rb:98:in `setup_method_substitute'
# /Users/proby/.gem/ruby/2.1.2/gems/rspec-mocks-3.5.0/lib/rspec/mocks/matchers/receive.rb:86:in `setup_mock_proxy_method_substitute'
# /Users/proby/.gem/ruby/2.1.2/gems/rspec-mocks-3.5.0/lib/rspec/mocks/matchers/receive.rb:43:in `setup_allowance'
# /Users/proby/.gem/ruby/2.1.2/gems/rspec-mocks-3.5.0/lib/rspec/mocks/targets.rb:46:in `define_matcher'
# /Users/proby/.gem/ruby/2.1.2/gems/rspec-mocks-3.5.0/lib/rspec/mocks/targets.rb:10:in `block in delegate_to'
# ./rspec_mocks_bug.rb:15:in `block (2 levels) in <top (required)>'
<TRIMMED>
Finished in 0.00433 seconds (files took 0.08962 seconds to load)
1 example, 1 failure
Failed examples:
rspec ./rspec_mocks_bug.rb:14 # TheExample shows the bug
I'm not really suggesting this needs to be fixed with all the smelly code and poor practices, just thought it was interesting edge case.