Skip to content

Instantly share code, notes, and snippets.

@tfwright
Created March 26, 2012 21:49
Show Gist options
  • Save tfwright/2210001 to your computer and use it in GitHub Desktop.
Save tfwright/2210001 to your computer and use it in GitHub Desktop.
Demonstrates unexpected behavior from .any_instance
require 'rspec/mocks'
describe Object do
it "should play nicely with dup" do
Object.any_instance.stub(:some_method)
o = Object.new
o.some_method
o.dup.some_method
end
it "should not care how many times a stubbed method is called" do
Object.any_instance.stub(:some_method)
Object.new.some_method
Object.new.some_method
end
it "should not care if same instance receives multiple calls" do
Object.any_instance.stub(:some_method)
o = Object.new
o.some_method
o.some_method
end
it "should explicitly not care how many times a stubbed method is called" do
Object.any_instance.stub(:some_method).any_number_of_times
Object.new.some_method
pending "doesn't allow second method call" do
Object.new.some_method
end
end
it "should allow a method to be called any number of times, at least once" do
Object.any_instance.should_receive(:some_method).at_least(:once)
Object.new.some_method
pending "doesn't allow second method call" do
Object.new.some_method
end
end
it "should allow a method to be called twice" do
Object.any_instance.should_receive(:some_method).at_least(2).times
Object.new.some_method
pending "doesn't allow second method call" do
Object.new.some_method
end
end
end
@tfwright
Copy link
Author

[master] {1.9.3-p125} gist-2210001 rspec rspec_any_instance_test.rb
FFF

Failures:

  1. Object should not care how many times a stubbed method is called
    Failure/Error: Object.any_instance.stub(:some_method).any_number_of_times
    NoMethodError:
    undefined method `include?' for nil:NilClass

    ./rspec_any_instance_test.rb:6:in`block (2 levels) in <top (required)>'

  2. Object should allow a method to be called any number of times, at least once
    Failure/Error: Object.new.some_method
    The message 'some_method' was received by #Object:0x007fee94261be8 but has already been received by #Object:0x007fee94267d40

    ./rspec_any_instance_test.rb:14:in `block (2 levels) in <top (required)>'

  3. Object should allow a method to be called twice
    Failure/Error: Object.new.some_method
    The message 'some_method' was received by #Object:0x007fee9424b078 but has already been received by #Object:0x007fee9425d9a8

    ./rspec_any_instance_test.rb:20:in `block (2 levels) in <top (required)>'

Finished in 0.00154 seconds
3 examples, 3 failures

Failed examples:

rspec ./rspec_any_instance_test.rb:5 # Object should not care how many times a stubbed method is called
rspec ./rspec_any_instance_test.rb:11 # Object should allow a method to be called any number of times, at least once
rspec ./rspec_any_instance_test.rb:17 # Object should allow a method to be called twice

@felipecsl
Copy link

I am having these problems right now with rspec 2.10. Are there any open bugs for these issues?

@paulmakepeace
Copy link

I just ran into this. I didn't dig in far but it seems stub doesn't allow setting up expectations on the number of times a method is called. should_receive however does.

@xaviershay
Copy link

open bug is rspec/rspec-mocks#119

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment