我想检查一个方法是否被调用完全(n)次,但我仍然希望该方法执行其原始功能。考虑一个简单的缩略图系统缓存缩略图文件,并确保ImageMagick的创建缩略图的“转换”可执行文件仅在第一个请求上被调用。rspec 2:检测调用方法,但仍然有它执行其功能
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
在我的情况下,我逃离了第一次尝试,但可能有情况下,我没有。第二个失败,因为检测到呼叫Thumbnail.convert
,但该方法本身不起任何作用。有没有什么办法来检测方法的调用,并让它做到原来的东西?
BTW:我怀疑这是question非常相似,但后来我得到了描述丢失,也很没有答案......
谢谢,像一个魅力工作! – thomax