2013-10-24 4 views
30

आरएसपीसी में, it_behaves_like और include_examples के बीच क्या अंतर है?"include_examples" और "it_behaves_like" के बीच क्या अंतर है?

documentation का कहना है:

include_examples - शामिल (रों)

it_behaves_like "name" वर्तमान संदर्भ में उदाहरण - शामिल (रों) एक नेस्टेड संदर्भ में उदाहरण

लेकिन क्या क्या इसका वास्तव में मतलब है? दूसरे के साथ एक को बदलने से ऐसा लगता है कि मेरे परीक्षण पास या असफल होते हैं या नहीं। क्या कुछ स्थितियों में एक दूसरे को पसंद करने का कोई कारण है?

इसके अलावा, it_should_behave_like और it_behaves_like समानार्थी शब्द हैं?

+1

कृपया उल्लेख द्वारा बनाई नेस्टेड संदर्भ के कारण से बचा जाता है it_behaves_like साथ include_examples को बदलने के लिए, अधिभावी इस तरह से विधि में है हो जाना चाहिए। caltech.edu/~mvanier/hacking/rants/cars.html, रूबी अनुभाग। –

उत्तर

28

शायद आप जानते हैं कि describe, context, it और specify का उपयोग अपने कोड के एक पहलू को स्पष्ट रूप से संवाद करने के लिए कैसे करें। पाठक के साथ इस संचार को बेहतर बनाने के लिए it_behaves_like द्वारा प्रदान किए गए नेस्टेड संदर्भ का उपयोग किया जा सकता है। आप --format documentation साथ RSpec चलाते हैं

shared_examples "a collection" do 
    context "initialized with 3 items" do 
    it "says it has three items" do 
     # ... 
    end 
    end 
end 

describe Array do 
    it_behaves_like "a collection" 
    include_examples "a collection" 
end 

आप निम्नलिखित उत्पादन हो:

मैं shared examples के लिए RSpec दस्तावेज में दिए गए उदाहरण पर मेरे उदाहरण का आधार होगा

Array 
    behaves like a collection 
    initialized with 3 items 
     says it has three items 
    initialized with 3 items 
    says it has three items 

तो फर्क है कैसे spec पढ़ा जाता है उदाहरण के लिए विफलता के मामले में।

आप कौन सी शैली पसंद करते हैं, यह सौंदर्यशास्त्र का सवाल है कि आप अपनी चश्मा को कैसे पढ़ना पसंद करते हैं। इसके अलावा आप हमेशा एक ही शैली का उपयोग करने का सुझाव देंगे यदि आप स्थिरता में सुधार करने के लिए एक टीम में काम करते हैं।


इसके अलावा, it_should_behave_like और it_behaves_like सिर्फ समानार्थक शब्द हैं?

लगभग, संदर्भ अलग-अलग नाम दिया गया है। it should behave like ... बनाम behaves like ...। फिर सौंदर्यशास्त्र का सवाल।

+3

तो स्पष्टीकरण के लिए: मैं किस तीन का उपयोग करता हूं इसका कोई प्रभाव नहीं पड़ता है कि कोई परीक्षण पास/असफल होगा, यह सौंदर्यशास्त्र/पठनीयता की पसंद है? – GMA

+0

@ जॉर्ज मॉमिलो बिल्कुल :) – jayeff

12

यदि आप साझा_एक्सएम्पल्स को पैरामीटर पास करते हैं तो एक अंतर है।

यह एक चेतावनी in their doc में बहुत अच्छी तरह से समझाया है:

चेतावनी: जब आप वर्तमान संदर्भ एकाधिक में पैरामिट्रीकृत उदाहरण बार शामिल, आप पिछले विधि परिभाषा और पिछले घोषणा जीत भी पार कर जाते। आप साझा उदाहरण (या साझा संदर्भ)

RSpec.shared_examples "some example" do |parameter| 
    \# Same behavior is triggered also with either `def something; 'some value'; end` 
    \# or `define_method(:something) { 'some value' }` 
    let(:something) { parameter } 
    it "uses the given parameter" do 
    expect(something).to eq(parameter) 
    end 
end 

RSpec.describe SomeClass do 
    include_examples "some example", "parameter1" 
    include_examples "some example", "parameter2" 
end 

आप वास्तव में यह कर रहे हैं इस तरह का है तो अगर (ध्यान दें कि पहला उदाहरण असफल हो जायेगी):

RSpec.describe SomeClass do 
    \# Reordered code for better understanding of what is happening 
    let(:something) { "parameter1" } 
    let(:something) { "parameter2" } 

    it "uses the given parameter" do 
    \# This example will fail because last let "wins" 
    expect(something).to eq("parameter1") 
    end 

    it "uses the given parameter" do 
    expect(something).to eq("parameter2") 
    end 
end 

इस तरह की सूक्ष्म त्रुटि को रोकने के लिए यदि आप उसी संदर्भ में एक ही नाम के साथ कई विधियों की घोषणा करते हैं तो चेतावनी उत्सर्जित होती है। http: //users.cms आप इस चेतावनी सरल समाधान के लिए it_behaves_like

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