结合 beAnInstanceOf 和 "aka" 时自定义匹配器中的 Specs2 奇怪行为

Specs2 strange behavior in custom matcher when combining beAnInstanceOf and "aka"

我遇到了奇怪的行为,我想知道这是一个错误还是我遗漏了什么。

以下代码:

class Foo extends SpecificationWithJUnit {

  "This test should pass" in new ctx {
   Bar(Zoo("id")) must haveInstanceZoo
 }

 trait ctx extends Scope {

   def haveInstanceZoo : Matcher[Bar] = 
     beAnInstanceOf[Zoo] ^^ { (_: Bar).z aka "instanceOfZoo" }
 }
}

case class Bar(z: Zoo)
case class Zoo(id: String)

失败并出现以下异常:

'org.specs2.matcher.ThrownExpectationsCreation$$anon@48072f8c: 
 org.specs2.matcher.ThrownExpectationsCreation$$anon' 
 is not an instance of 'com.test.Zoo'

如果我从自定义匹配器中删除 "aka",一切正常。

想法?

谢谢 内塔

您不能像这样使用 aka,因为您实际上是在尝试断言 Expectation,您使用 aka 创建的对象是 Zoo 的实例。

如果您想在匹配器上指定不同的失败消息,您可以这样写:

def haveInstanceZoo: Matcher[Bar] = (bar: Bar) =>
  (bar.z.isInstanceOf[Zoo], "bar.z is not an instance of Zoo")