Created
July 21, 2009 09:46
-
-
Save dchelimsky/151226 to your computer and use it in GitHub Desktop.
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 Thing | |
def self.find_something(inputs) | |
# .. do stuff here | |
end | |
def self.add_something(inputs) | |
# .. do stuff here | |
end | |
def self.find_or_add_something(inputs) | |
if x = self.find_something(inputs) | |
x | |
else | |
self.add_something(inputs) | |
self.find_something(inputs) | |
end | |
end | |
end | |
describe Thing do | |
describe "#find_or_add_something" do | |
def thing | |
@thing ||= stub() | |
end | |
before(:each) do | |
Thing.stub(:find_something) | |
Thing.stub(:add_something) | |
end | |
it "passes inputs to find_something" do | |
Thing.should_receive(:find_something). | |
with(:this => 'data'). | |
and_return(thing) | |
Thing.find_or_add_something(:this => 'data') | |
end | |
context "when find_something returns a Thing" do | |
it "returns the Thing" do | |
Thing.stub(:find_something).and_return(thing) | |
Thing.find_or_add_something(:this => 'data'). | |
should equal(thing) | |
end | |
end | |
context "when find_something returns nil" do | |
it "calls add_something with the given inputs" do | |
Thing.should_receive(:add_something). | |
with(:this => 'data') | |
Thing.find_or_add_something(:this => 'data') | |
end | |
it "calls find_something with the given inputs" do | |
Thing.should_receive(:find_something). | |
with(:this => 'data') | |
Thing.find_or_add_something(:this => 'data') | |
end | |
it "returns the result of find_something" do | |
Thing.stub(:find_something).and_return(nil, thing) | |
Thing.find_or_add_something(:this => 'data'). | |
should equal(thing) | |
end | |
end | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment