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
@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