2011-08-26 4 views
12

निष्पादित करता है, मैं यह जांचना चाहता हूं कि कोई विधि बिल्कुल (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 बहुत समान है, लेकिन फिर मैं वर्णन में खो और यह भी अनुत्तरित है ...

उत्तर

20

अब उपयोग के इस मामले एक and_call_original विधि ठीक है। (RSpec 2,12)

Sinatra::Thumbnails.should_receive(:convert).and_call_original 

प्रलेखन जोआओ, here द्वारा संदर्भित एक ही पृष्ठ पर पाया जा सकता है।

यह भी देखें: changelog

+0

धन्यवाद, एक आकर्षण की तरह काम किया! – thomax

15

आहा! मुझे लगता है कि मैंने हल निकाल लिया!

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 

यह बहुत अंत में here में (खराब, मेरी राय में) दस्तावेज़ीकरण किया गया है ...

+0

प्रलेखन वास्तव में गरीब है, और मैं पेज आप की ओर इशारा में 'original_method' के किसी भी उल्लेख नहीं मिल सकता है। लेकिन जवाब के लिए धन्यवाद! – lulalala

+0

'original_method' केवल एक स्थानीय चर है जिसका मैंने उपयोग किया था! जिस पृष्ठ से मैंने लिंक किया है वह "मनमानी हैंडलिंग" का उल्लेख करता है, जो मुझे उस स्थानीय चर में संग्रहीत विधि को कॉल करने के लिए आवश्यक है। –

+0

मैं क्या सोच रहा था? क्षमा करें, मैंने इसे ठीक से नहीं पढ़ा :( – lulalala

संबंधित मुद्दे