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...
Now there's an and_call_original
method precisely for this use case. (RSpec 2.12)
Sinatra::Thumbnails.should_receive(:convert).and_call_original
The documentation can be found on the same page referenced by Joao, here.
See also: the changelog
Yay! I think I figured it out!
it "should detect a cached version" do
original_method = Sinatra::Thumbnails.method(:convert)
Sinatra::Thumbnails.should_receive(:convert).exactly(1).times do |*args|
original_method.call(*args)
end
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
It's documented (poorly, in my opinion) in here at the very end...
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With