Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
56 views
in Technique[技术] by (71.8m points)

rspec2 - rspec 2: detect call to method but still have it perform its function

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...