Scala specs2与“aka”匹配

时间:2017-07-19 21:29:26

标签: scala specs2

我想在使用“aka”提供标签时检查字符串是否包含另一个字符串。例如:

"31 west 23rd street, NY" aka "address"  must contain("11065")

失败
address '31 west 23rd street, NY' doesn't contain '11065'.

但是,我想指定11066是一个邮政编码。即:

"31 west 23rd street, NY" aka "address"  must contain("11065") aka "zip code"

哪个不起作用。

任何想法如何实现? 我期望的结果是:

address '31 west 23rd street, NY' doesn't contain zip code '11065'.

下面是一个可能的解决方案,但我不喜欢它,因为它不是spec2 native,只支持字符串:

def contain(needle: String, aka: String) = new Matcher[String] {
  def apply[S <: String](b: Expectable[S]) = {
    result(needle != null && b.value != null && b.value.contains(needle),
      s"${b.description} contains $aka '$needle'",
      s"${b.description} doesn't contain $aka '$needle'", b)
  }
}

1 个答案:

答案 0 :(得分:1)

我认为没有适用于所有匹配器的解决方案。在这种情况下,您可以重复使用aka机器

def contain(expected: Expectable[String]): Matcher[String] = new Matcher[String] {
  def apply[S <: String](e: Expectable[S]): MatchResult[S] =
    result(e.value.contains(expected.value),
      s" ${e.value} contains ${expected.description} ${expected.value}",
      s" ${e.value} does not contain ${expected.description}",
      e)
}

"31 west 23rd street, NY" aka "address"  must contain("11065" aka "the zip code")

显示

 31 west 23rd street, NY does not contain the zip code '11065'