Skip to content

Instantly share code, notes, and snippets.

@lenny
Created December 1, 2009 16:28
Show Gist options
  • Save lenny/246407 to your computer and use it in GitHub Desktop.
Save lenny/246407 to your computer and use it in GitHub Desktop.
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