Last active
January 4, 2016 10:39
-
-
Save hmayer00/8610008 to your computer and use it in GitHub Desktop.
Demonstrating problems with any_instance.should_receive().with corrupting subsequent tests
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
require 'spec_helper' | |
# These specs demonstrate that any_instance.should_receive().with will break subsequent specs if | |
# the message is received without the expected argument | |
describe "should_receive corruption", :focus do | |
class Foo | |
def bar(options = {}) | |
end | |
end | |
let(:foo) { Foo.new } | |
# These work fine | |
context "when not using .with" do | |
context "when the message is received" do | |
it "will pass the first time" do | |
foo.bar | |
foo.should be | |
end | |
it "will receive this message" do | |
Foo.any_instance.should_receive(:bar) | |
foo.bar | |
end | |
it "will pass the second time because the message was received" do | |
foo.bar | |
foo.should be | |
end | |
end | |
context "when the message is not received" do | |
it "will pass the first time" do | |
foo.bar | |
foo.should be | |
end | |
it "should fail because it will not receive this message" do | |
Foo.any_instance.should_receive(:bar) | |
end | |
it "will pass the second time, as it should" do | |
foo.bar | |
foo.should be | |
end | |
end | |
end | |
context "when using .with" do | |
# These work fine | |
context "when the message is received" do | |
it "will pass the first time" do | |
foo.bar | |
foo.should be | |
end | |
it "will receive this message" do | |
Foo.any_instance.should_receive(:bar).with(anything) | |
foo.bar({}) | |
end | |
it "will pass the second time" do | |
foo.bar | |
foo.should be | |
end | |
end | |
# These work fine | |
context "when the message is not received" do | |
it "will pass the first time" do | |
foo.bar | |
foo.should be | |
end | |
it "should fail because it will not receive this message" do | |
Foo.any_instance.should_receive(:bar).with(anything) | |
end | |
it "will pass the second time" do | |
foo.bar | |
foo.should be | |
end | |
end | |
# This is where you'll see the problem | |
context "when the message is received with the wrong parameter" do | |
it "will pass the first time" do | |
foo.bar | |
foo.should be | |
end | |
it "should fail because it will receive this message with the wrong parameters" do | |
Foo.any_instance.should_receive(:bar).with(anything) | |
foo.bar | |
end | |
# this will fail with "The message 'bar' was received by #<Foo:0x007f87120be0a0> but has already been received by" | |
# presumably the should_receive monitor didn't get cleared? | |
it "should pass the second time, but doesn't" do | |
foo.bar | |
foo.should be | |
end | |
end | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment