Skip to content

Instantly share code, notes, and snippets.

@JonathonMA
Created December 12, 2013 23:27
Show Gist options
  • Save JonathonMA/7937446 to your computer and use it in GitHub Desktop.
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.
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
@JonathonMA
Copy link
Author

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:

ThingThatDoesThings
  should ask the Collaborator what do do
  should be bad at stubbing

BrokenThingThatDoesThings
  should ask the Collaborator what do do (FAILED - 1)
  should be bad at stubbing

And that's why you always check your stub parameters.

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