2016-06-17 9 views
7

1つの文字列に多数の部分文字列が含まれていることを確認する必要があります。次の作品文字列にはScalaTest Matcherの多くの部分文字列が含まれています

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

しかし、それは3つのほぼ重複した行がかかります。しかし私は、これがうまくいかない...文字列は確かにこれらのサブストリングが含まれていながら、アサーションは単に失敗し

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

のようなものを探しています。

このようなアサーションを1行で実行するにはどうすればよいですか?

答えて

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を参照してください。

関連する問題