I want to check if a method was called exactly(n) times, but I still want that method to perform its original function. Consider a simple thumbnailing system that caches the thumbnail file and make sure ImageMagick's "convert" executable that creates the thumbnail is only called on the first request.
it "this passes: should detect a cached version" do
thumbnail_url = thumbnail_url_for("images/something.jpg")
get thumbnail_url
last_response.should be_ok
Sinatra::Thumbnail.should_not_receive(:convert)
get thumbnail_url
last_response.should be_ok
end
it "this fails: should detect a cached version" do
Sinatra::Thumbnail.should_receive(:convert).exactly(1).times
thumbnail_url = thumbnail_url_for("images/something.jpg")
get thumbnail_url
last_response.should be_ok
get thumbnail_url
last_response.should be_ok
end
In my case I get away with my first attempt, but there could be cases where I don't. The second one fails because the call Thumbnail.convert
is detected but the method itself doesn't do anything. Is there some way to just detect the call to the method and have it do it's original thing?
BTW: I suspect this question is very similar, but then I get lost in the description and also it's unanswered...
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…