Created
December 1, 2009 16:28
-
-
Save lenny/246407 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 ExpensiveOperation | |
def self.lookup(question) | |
"This goes to the database" | |
end | |
end | |
class UsesExpensiveOperation | |
def initialize | |
@cache = {} | |
end | |
def answer_for(question) | |
# oops, this is broken, it was supposed to be cached | |
# @cache[question] ||= ExpensiveOperation.lookup(question) | |
@cache[question] = ExpensiveOperation.lookup(question) | |
end | |
end | |
describe "suprising behavior" do | |
before do | |
@object = UsesExpensiveOperation.new | |
ExpensiveOperation.stub(:lookup).and_return('whatever') | |
end | |
it "should fail because I'm specifying that lookup should be called at most one time, but that's not true" do | |
ExpensiveOperation.should_receive(:lookup).at_most(:once).and_return('an answer') | |
@object.answer_for("my question") | |
@object.answer_for("my question") | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment