आप स्पॉक में हैमक्रिस्ट समर्थन का उपयोग कर सकते हैं और इस मामले के लिए स्पष्ट रूप से डिजाइन किए गए Matcher
का उपयोग कर सकते हैं - containsInAnyOrder
। आप नीचे दिए गए आयात की जरूरत है: इस प्रकार
import static org.hamcrest.Matchers.containsInAnyOrder
import static spock.util.matcher.HamcrestSupport.that
तो फिर आप अपने परीक्षण कोड लिख सकते हैं:
given:
def companyList = [ "Second", "First"]
expect:
that companyList, containsInAnyOrder("First", "Second")
इस सूची में है कि नकल तत्वों को सही ढंग से विचार किया जाएगा में .sort()
का उपयोग करने पर लाभ है। निम्न परीक्षण Hamcrest का उपयोग कर असफल हो जायेगी लेकिन .sort()
given:
def companyList = [ "Second", "First", "Second"]
expect:
that companyList, containsInAnyOrder("First", "Second")
Condition not satisfied:
that companyList, containsInAnyOrder("First", "Second")
| |
| [Second, First, Second]
false
Expected: iterable over ["First", "Second"] in any order
but: Not matched: "Second"
का उपयोग कर उत्तीर्ण आप expect:
के बजाय then:
उपयोग कर रहे हैं आप पठनीयता के लिए that
आयात की expect
बजाय उपयोग कर सकते हैं।
then:
expect companyList, containsInAnyOrder("First", "Second")
मैंने कभी भी किसी परेशानी के रूप में .sort() का उपयोग करने का विचार नहीं किया है। यह काफी terse है। – th3morg