Mic*_*das 12 string scala matcher scalatest
我需要检查一个字符串是否包含许多子字符串.以下作品
string should include ("seven")
string should include ("eight")
string should include ("nine")
Run Code Online (Sandbox Code Playgroud)
但它需要三条几乎重复的线条.我正在寻找类似的东西
string should contain allOf ("seven", "eight", "nine")
Run Code Online (Sandbox Code Playgroud)
但这不起作用...断言只是失败,而字符串肯定包含这些子串.
我怎样才能在一行中执行这样的断言?
Ser*_*gey 21
试试这个:
string should (include("seven") and include("eight") and include("nine"))
Run Code Online (Sandbox Code Playgroud)
您始终可以创建自定义匹配器:
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(", ")}""")
}
Run Code Online (Sandbox Code Playgroud)
有关详细信息,请参阅http://www.scalatest.org/user_guide/using_matchers#usingCustomMatchers.
| 归档时间: |
|
| 查看次数: |
4640 次 |
| 最近记录: |