2011-03-27 9 views
7

का उपयोग कर चेतावनियों के लिए परीक्षण क्या यह किसी भी तरह से आरएसपीईसी का उपयोग कर रूबी की चेतावनी का परीक्षण करना संभव है?आरएसपीसी

इस तरह:

class MyClass 
    def initialize 
    warn "Something is wrong" 
    end 
end 

it "should warn" do 
    MyClass.new.should warn("Something is wrong") 
end 

उत्तर

15

warnKernel है, जो हर वस्तु में शामिल है में परिभाषित किया गया है। आप प्रारंभ दौरान चेतावनी को ऊपर उठाने नहीं कर रहे थे, तो आप इस तरह एक चेतावनी निर्दिष्ट कर सकते हैं:

obj = SomeClass.new 
obj.should_receive(:warn).with("Some Message") 
obj.method_that_warns 

एक चेतावनी initialize विधि में उठाया spec'ing काफी ज्यादा जटिल है। यदि यह किया जाना चाहिए, तो आप $stderr के लिए नकली IO ऑब्जेक्ट में स्वैप कर सकते हैं और इसका निरीक्षण कर सकते हैं। बस उदाहरण

class MyClass 
    def initialize 
    warn "Something is wrong" 
    end 
end 

describe MyClass do 
    before do 
    @orig_stderr = $stderr 
    $stderr = StringIO.new 
    end 

    it "warns on initialization" do 
    MyClass.new 
    $stderr.rewind 
    $stderr.string.chomp.should eq("Something is wrong") 
    end 

    after do 
    $stderr = @orig_stderr 
    end 
end 
+0

तुम क्या कर सकते हैं 'SomeClass.allocate' बल्कि' SomeClass से .new', और फिर इसे चाहिए_receive, और फिर शुरू करें? –

+0

'प्रारंभिक' में चेतावनियों के लिए मैंने जिस अन्य दृष्टिकोण का उपयोग किया है, वह है कि मेरी कक्षा कॉल 'कर्नेल.वर्न' स्पष्ट रूप से (केवल 'चेतावनी' के बजाय) है। इसे कर्नेल पर बुलाया जाने की आवश्यकता नहीं है; इसे कुछ वैश्विक पर बुलाया जाना चाहिए कि मैं तत्कालता पर 'must_receive' सेट कर सकता हूं। –

4

जो वास्तव में आपकी समस्या नहीं सुलझती कस्टम उम्मीद के साथ एक अच्छा लेख है के बाद इसे फिर से स्थापित करना न भूलें: http://greyblake.com/blog/2012/12/14/custom-expectations-with-rspec/

तो यह चाहते हैं: उस पर

expect { MyClass.new }.to write("Something is wrong").to(:error) 

बेस आलेख आप इसे इस तरह उपयोग करने की अपनी उम्मीद बना सकते हैं:

expect { MyClass.new }.to warn("Something is wrong") 
+2

यह एक बहुत ही शानदार जवाब है, लेकिन अगर लेख नीचे चला जाता है तो मैं लेख में बड़े पैमाने पर लेख डालने की सलाह दूंगा। – sunnyrjuneja

0

यह मेरा समाधान, मैं एक कस्टम मिलान has_warn को परिभाषित किया गया है

require 'rspec' 
require 'stringio' 

module CustomMatchers 
    class HasWarn 
    def initialize(expected) 
     @expected = expected 
    end 

    def matches?(given_proc) 
     original_stderr = $stderr 
     $stderr = StringIO.new 
     given_proc.call 
     @buffer = $stderr.string.strip 
     @expected.include? @buffer.strip 
    ensure 
     $stderr = original_stderr 
    end 

    def supports_block_expectations? 
     true 
    end 

    def failure_message_generator(to) 
     %Q[expected #{to} get message:\n#{@expected.inspect}\nbut got:\n#{@buffer.inspect}] 
    end 

    def failure_message 
     failure_message_generator 'to' 
    end 

    def failure_message_when_negated 
     failure_message_generator 'not to' 
    end 

    end 

    def has_warn(msg) 
    HasWarn.new(msg) 
    end 
end 
अब

आप इस समारोह का पालन के रूप में उपयोग कर सकते हैं CustomMatchers शामिल बाद:

expect{ MyClass.new }.to has_warn("warning messages")