Skip to content

Instantly share code, notes, and snippets.

@alindeman
Last active December 15, 2015 18:29
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save alindeman/89ec035c97d161c89183 to your computer and use it in GitHub Desktop.
Save alindeman/89ec035c97d161c89183 to your computer and use it in GitHub Desktop.
# Original mock syntax, straight forward example
Object.any_instance.should_receive(:foo)
Object.new.foo # satisfies mock
# Original mock syntax, confusing example b/c of "any" term
Object.any_instance.should_receive(:foo)
Object.new.foo # satisfies mock
Object.new.foo # error, "another instance has already received the :foo message"
# Proposed syntaxes
expect_any_instance_of(Object).to receive(:foo)
# ... or something like ...
expect_an_instance_of(Object).to receive(:foo)
expect_one_instance_of(Object).to receive(:foo)
# Original stub syntax, straight forward example
Object.any_instance.stub(:foo).and_return(:bar)
Object.new.foo # => :bar
# Original stub syntax, confusing example b/c of "any" term
Object.any_instance.stub(:foo).and_return(1, 2, 3)
o = Object.new
o.foo # => 1
o.foo # => 2
Object.new.foo # => 1 (NOT 3)
# Proposed syntaxes
allow_any_instance_of(Object).to receive(:foo).and_return(:bar)
# ... or something like ...
allow_every_instance_of(Object).to receive(:foo).and_return(:bar)
@stevenharman
Copy link

expect(Object).every_instance.to receive(:foo)

expect(Object).an_instance.to receive(:foo)

Though the an_instance is still confusing in the case that you create multiple instances.

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