2016-06-17 10 views
7

में कई सबस्ट्रिंग शामिल हैं, मुझे यह जांचना होगा कि एक स्ट्रिंग में कई सबस्ट्रिंग हैं। निम्नलिखित कामस्ट्रिंग में स्कैलाटेस्ट मैचर्स

string should include ("seven") 
string should include ("eight") 
string should include ("nine") 

लेकिन इसमें लगभग तीन डुप्लिकेट लाइनें होती हैं। मैं की तरह

string should contain allOf ("seven", "eight", "nine") 

लेकिन यह काम नहीं करता कुछ के लिए देख रहा हूँ ... दावे सिर्फ जबकि स्ट्रिंग यकीन है कि के लिए इन सबस्ट्रिंग शामिल विफल रहता है।

मैं एक पंक्ति में ऐसे दावे कैसे कर सकता हूं?

उत्तर

8

इस प्रयास करें:

string should (include("seven") and include("eight") and include("nine")) 
5

तुम हमेशा एक कस्टम मिलान बना सकते हैं:

it should "..." in { 
    "str asd dsa ddsd" should includeAllOf ("r as", "asd", "dd") 
} 

def includeAllOf(expectedSubstrings: String*): Matcher[String] = 
    new Matcher[String] { 
    def apply(left: String): MatchResult = 
     MatchResult(expectedSubstrings forall left.contains, 
     s"""String "$left" did not include all of those substrings: ${expectedSubstrings.map(s => s""""$s"""").mkString(", ")}""", 
     s"""String "$left" contained all of those substrings: ${expectedSubstrings.map(s => s""""$s"""").mkString(", ")}""") 
    } 

अधिक जानकारी के लिए http://www.scalatest.org/user_guide/using_matchers#usingCustomMatchers देखें।

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