Created
December 12, 2013 23:27
-
-
Save JonathonMA/7937446 to your computer and use it in GitHub Desktop.
Demonstration of why checking your stub parameters is important if you value having.
This file contains hidden or 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
class Collaborator | |
# not implemented yet because TDD! | |
end | |
class ThingThatDoesThings | |
def initialize name | |
@name = name | |
end | |
def do_thing with | |
Collaborator.new.do_a_thing(@name, with) | |
end | |
end | |
class BrokenThingThatDoesThings < ThingThatDoesThings | |
def do_thing with | |
Collaborator.new.do_a_thing(with, @name) | |
end | |
end | |
describe ThingThatDoesThings do | |
it "should ask the Collaborator what do do" do | |
combined_thing = "name-var" | |
name = "name" | |
var = "var" | |
allow_any_instance_of(Collaborator).to receive(:do_a_thing) | |
.with(name, var).and_return(combined_thing) | |
thing = ThingThatDoesThings.new name | |
expect(thing.do_thing(var)).to eq combined_thing | |
end | |
it "should be bad at stubbing" do | |
combined_thing = "name-var" | |
name = "name" | |
var = "var" | |
allow_any_instance_of(Collaborator).to receive(:do_a_thing) | |
.and_return(combined_thing) | |
thing = ThingThatDoesThings.new name | |
expect(thing.do_thing(var)).to eq combined_thing | |
end | |
end | |
describe BrokenThingThatDoesThings do | |
it "should ask the Collaborator what do do" do | |
combined_thing = "name-var" | |
name = "name" | |
var = "var" | |
allow_any_instance_of(Collaborator).to receive(:do_a_thing) | |
.with(name, var).and_return(combined_thing) | |
thing = BrokenThingThatDoesThings.new name | |
expect(thing.do_thing(var)).to eq combined_thing | |
end | |
it "should be bad at stubbing" do | |
combined_thing = "name-var" | |
name = "name" | |
var = "var" | |
allow_any_instance_of(Collaborator).to receive(:do_a_thing) | |
.and_return(combined_thing) | |
thing = BrokenThingThatDoesThings.new name | |
expect(thing.do_thing(var)).to eq combined_thing | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
So obviously the Broken* spec should always fail, however, because we failed to check the parameters of the stub in the bad stubbing scenario, it passes:
And that's why you always check your stub parameters.