Rspec-mocks有expect(some_object).to receive(:some_method).and_call_original.我可以用摩卡这样做,如果是的话,怎么样?some_object.expects(:some_method).......什么?
我想检查一个方法是否被完全(n)次调用,但我仍然希望该方法执行其原始函数.考虑一个简单的缩略图系统来缓存缩略图文件,并确保只在第一个请求时调用创建缩略图的ImageMagick的"转换"可执行文件.
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
Run Code Online (Sandbox Code Playgroud)
在我的情况下,我第一次尝试逃脱,但可能有一些我不这样做的情况.第二个失败是因为Thumbnail.convert检测到了调用,但方法本身没有做任何事情.有没有办法只检测对方法的调用并让它做原始的事情?
顺便说一句:我怀疑这个问题非常相似,但后来我在描述中迷失了,也没有答案......