Skip to content

Instantly share code, notes, and snippets.

@joshuap
Last active April 20, 2016 15:54
Show Gist options
  • Save joshuap/4561dff8af653d1f6a3862557d15c503 to your computer and use it in GitHub Desktop.
Save joshuap/4561dff8af653d1f6a3862557d15c503 to your computer and use it in GitHub Desktop.
RSpec expect_any_instance_of inheritance bug
class ExampleBase
def foo(arg)
true
end
end
class ExampleObj < ExampleBase
def foo(arg)
super
end
end
context "when subclass is object of expect_any_instance_of" do
before do
allow_any_instance_of(ExampleBase).to receive(:foo)
end
it "blows up" do
expect_any_instance_of(ExampleObj).to receive(:foo).with(:bar)
ExampleObj.new.foo(:bar)
end
end
source 'https://rubygems.org'
gem 'rspec', '3.4.0'
GEM
remote: https://rubygems.org/
specs:
diff-lcs (1.2.5)
rspec (3.4.0)
rspec-core (~> 3.4.0)
rspec-expectations (~> 3.4.0)
rspec-mocks (~> 3.4.0)
rspec-core (3.4.4)
rspec-support (~> 3.4.0)
rspec-expectations (3.4.0)
diff-lcs (>= 1.2.0, < 2.0)
rspec-support (~> 3.4.0)
rspec-mocks (3.4.1)
diff-lcs (>= 1.2.0, < 2.0)
rspec-support (~> 3.4.0)
rspec-support (3.4.1)
PLATFORMS
ruby
DEPENDENCIES
rspec (= 3.4.0)
BUNDLED WITH
1.11.2
F
Failures:
1) when subclass is object of expect_any_instance_of blows up
Failure/Error: expect_any_instance_of(ExampleObj).to receive(:foo).with(:bar)
NameError:
method `__foo_without_any_instance__' not defined in ExampleObj
# /usr/local/var/rbenv/versions/2.2.2/lib/ruby/gems/2.2.0/gems/rspec-mocks-3.4.1/lib/rspec/mocks/any_instance/recorder.rb:197:in `remove_method'
# /usr/local/var/rbenv/versions/2.2.2/lib/ruby/gems/2.2.0/gems/rspec-mocks-3.4.1/lib/rspec/mocks/any_instance/recorder.rb:197:in `block in restore_original_method!'
# /usr/local/var/rbenv/versions/2.2.2/lib/ruby/gems/2.2.0/gems/rspec-mocks-3.4.1/lib/rspec/mocks/any_instance/recorder.rb:194:in `class_exec'
# /usr/local/var/rbenv/versions/2.2.2/lib/ruby/gems/2.2.0/gems/rspec-mocks-3.4.1/lib/rspec/mocks/any_instance/recorder.rb:194:in `restore_original_method!'
# /usr/local/var/rbenv/versions/2.2.2/lib/ruby/gems/2.2.0/gems/rspec-mocks-3.4.1/lib/rspec/mocks/any_instance/recorder.rb:184:in `restore_method!'
# /usr/local/var/rbenv/versions/2.2.2/lib/ruby/gems/2.2.0/gems/rspec-mocks-3.4.1/lib/rspec/mocks/any_instance/recorder.rb:144:in `stop_observing!'
# /usr/local/var/rbenv/versions/2.2.2/lib/ruby/gems/2.2.0/gems/rspec-mocks-3.4.1/lib/rspec/mocks/any_instance/recorder.rb:230:in `observe!'
# /usr/local/var/rbenv/versions/2.2.2/lib/ruby/gems/2.2.0/gems/rspec-mocks-3.4.1/lib/rspec/mocks/any_instance/recorder.rb:63:in `should_receive'
# /usr/local/var/rbenv/versions/2.2.2/lib/ruby/gems/2.2.0/gems/rspec-mocks-3.4.1/lib/rspec/mocks/any_instance/proxy.rb:80:in `perform_proxying'
# /usr/local/var/rbenv/versions/2.2.2/lib/ruby/gems/2.2.0/gems/rspec-mocks-3.4.1/lib/rspec/mocks/any_instance/proxy.rb:62:in `should_receive'
# /usr/local/var/rbenv/versions/2.2.2/lib/ruby/gems/2.2.0/gems/rspec-mocks-3.4.1/lib/rspec/mocks/matchers/receive.rb:96:in `setup_method_substitute'
# /usr/local/var/rbenv/versions/2.2.2/lib/ruby/gems/2.2.0/gems/rspec-mocks-3.4.1/lib/rspec/mocks/matchers/receive.rb:89:in `setup_any_instance_method_substitute'
# /usr/local/var/rbenv/versions/2.2.2/lib/ruby/gems/2.2.0/gems/rspec-mocks-3.4.1/lib/rspec/mocks/matchers/receive.rb:45:in `setup_any_instance_expectation'
# /usr/local/var/rbenv/versions/2.2.2/lib/ruby/gems/2.2.0/gems/rspec-mocks-3.4.1/lib/rspec/mocks/targets.rb:45:in `define_matcher'
# /usr/local/var/rbenv/versions/2.2.2/lib/ruby/gems/2.2.0/gems/rspec-mocks-3.4.1/lib/rspec/mocks/targets.rb:14:in `block in delegate_to'
# ./bug_spec.rb:19:in `block (2 levels) in <top (required)>'
# /usr/local/var/rbenv/versions/2.2.2/lib/ruby/gems/2.2.0/gems/rspec-core-3.4.4/lib/rspec/core/example.rb:236:in `instance_exec'
# /usr/local/var/rbenv/versions/2.2.2/lib/ruby/gems/2.2.0/gems/rspec-core-3.4.4/lib/rspec/core/example.rb:236:in `block in run'
# /usr/local/var/rbenv/versions/2.2.2/lib/ruby/gems/2.2.0/gems/rspec-core-3.4.4/lib/rspec/core/example.rb:478:in `block in with_around_and_singleton_context_hooks'
# /usr/local/var/rbenv/versions/2.2.2/lib/ruby/gems/2.2.0/gems/rspec-core-3.4.4/lib/rspec/core/example.rb:435:in `block in with_around_example_hooks'
# /usr/local/var/rbenv/versions/2.2.2/lib/ruby/gems/2.2.0/gems/rspec-core-3.4.4/lib/rspec/core/hooks.rb:478:in `block in run'
# /usr/local/var/rbenv/versions/2.2.2/lib/ruby/gems/2.2.0/gems/rspec-core-3.4.4/lib/rspec/core/hooks.rb:616:in `run_around_example_hooks_for'
# /usr/local/var/rbenv/versions/2.2.2/lib/ruby/gems/2.2.0/gems/rspec-core-3.4.4/lib/rspec/core/hooks.rb:478:in `run'
# /usr/local/var/rbenv/versions/2.2.2/lib/ruby/gems/2.2.0/gems/rspec-core-3.4.4/lib/rspec/core/example.rb:435:in `with_around_example_hooks'
# /usr/local/var/rbenv/versions/2.2.2/lib/ruby/gems/2.2.0/gems/rspec-core-3.4.4/lib/rspec/core/example.rb:478:in `with_around_and_singleton_context_hooks'
# /usr/local/var/rbenv/versions/2.2.2/lib/ruby/gems/2.2.0/gems/rspec-core-3.4.4/lib/rspec/core/example.rb:233:in `run'
# /usr/local/var/rbenv/versions/2.2.2/lib/ruby/gems/2.2.0/gems/rspec-core-3.4.4/lib/rspec/core/example_group.rb:581:in `block in run_examples'
# /usr/local/var/rbenv/versions/2.2.2/lib/ruby/gems/2.2.0/gems/rspec-core-3.4.4/lib/rspec/core/example_group.rb:577:in `map'
# /usr/local/var/rbenv/versions/2.2.2/lib/ruby/gems/2.2.0/gems/rspec-core-3.4.4/lib/rspec/core/example_group.rb:577:in `run_examples'
# /usr/local/var/rbenv/versions/2.2.2/lib/ruby/gems/2.2.0/gems/rspec-core-3.4.4/lib/rspec/core/example_group.rb:543:in `run'
# /usr/local/var/rbenv/versions/2.2.2/lib/ruby/gems/2.2.0/gems/rspec-core-3.4.4/lib/rspec/core/runner.rb:119:in `block (3 levels) in run_specs'
# /usr/local/var/rbenv/versions/2.2.2/lib/ruby/gems/2.2.0/gems/rspec-core-3.4.4/lib/rspec/core/runner.rb:119:in `map'
# /usr/local/var/rbenv/versions/2.2.2/lib/ruby/gems/2.2.0/gems/rspec-core-3.4.4/lib/rspec/core/runner.rb:119:in `block (2 levels) in run_specs'
# /usr/local/var/rbenv/versions/2.2.2/lib/ruby/gems/2.2.0/gems/rspec-core-3.4.4/lib/rspec/core/configuration.rb:1680:in `with_suite_hooks'
# /usr/local/var/rbenv/versions/2.2.2/lib/ruby/gems/2.2.0/gems/rspec-core-3.4.4/lib/rspec/core/runner.rb:118:in `block in run_specs'
# /usr/local/var/rbenv/versions/2.2.2/lib/ruby/gems/2.2.0/gems/rspec-core-3.4.4/lib/rspec/core/reporter.rb:77:in `report'
# /usr/local/var/rbenv/versions/2.2.2/lib/ruby/gems/2.2.0/gems/rspec-core-3.4.4/lib/rspec/core/runner.rb:117:in `run_specs'
# /usr/local/var/rbenv/versions/2.2.2/lib/ruby/gems/2.2.0/gems/rspec-core-3.4.4/lib/rspec/core/runner.rb:93:in `run'
# /usr/local/var/rbenv/versions/2.2.2/lib/ruby/gems/2.2.0/gems/rspec-core-3.4.4/lib/rspec/core/runner.rb:78:in `run'
# /usr/local/var/rbenv/versions/2.2.2/lib/ruby/gems/2.2.0/gems/rspec-core-3.4.4/lib/rspec/core/runner.rb:45:in `invoke'
# /usr/local/var/rbenv/versions/2.2.2/lib/ruby/gems/2.2.0/gems/rspec-core-3.4.4/exe/rspec:4:in `<top (required)>'
# /usr/local/var/rbenv/versions/2.2.2/bin/rspec:23:in `load'
# /usr/local/var/rbenv/versions/2.2.2/bin/rspec:23:in `<main>'
Finished in 0.01333 seconds (files took 0.13913 seconds to load)
1 example, 1 failure
Failed examples:
rspec ./bug_spec.rb:18 # when sublcass is object of expect_any_instance_of blows up
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment