I have an expensive method called calculate_total . I need a method called total that will return the result of calculate_total . Subsequent calls to total should return the previous result of calculate_total .
I want to do this in test mode. Here are my tests (I use RSpec):
describe Item do describe "total" do before do @item = Item.new @item.stub!(:calculate_total => 123) end it "returns the calculated total" do @item.total.should == 123 end it "subsequent calls return the original result" do previous_total = @item.total @item.total.should equal(previous_total) end end end
This is not a good test because the following method skips the tests, but I expected the second test to fail:
def total calculate_total end
The reason calculate_total returns a Fixnum , so ruby ββdoes not see the result as 2 different objects. I was expecting the second test to fail, so I could do the following to pass:
def total @total ||= calculate_total end
Does anyone know a better way to test this?
I don't think this is the best / correct way to test it, but I decided: https://gist.github.com/1207270
source share