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