Skip to content

Instantly share code, notes, and snippets.

@kaiwren
Created March 25, 2011 13:50
Show Gist options
  • Save kaiwren/886849 to your computer and use it in GitHub Desktop.
Save kaiwren/886849 to your computer and use it in GitHub Desktop.
any_instance behaves like at_least_one_instance for expectations and like every_instance for stubs in mocha
require "spec_helper"
describe "any_instance in mocha" do
it "should bar" do
Array.any_instance.stubs(:foo).returns(1)
[].foo.should eq(1) # Every instance of Array seems to be
[].foo.should eq(1) # stubbed (as one would expect)
end
it "should foo" do
Array.any_instance.expects(:foo)
[].foo
[] # In contrast, this line doesn't fail while I would expect it to
end
end
# Long story short I see two distinct approaches for stubs and mocks conflated under the 'any_instance' banner
# Perhaps the approach used with stubs is better named 'every_instance' and the one used for expectations
# 'at_least_one_instance'?
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment