Search code examples
specs2

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


I'm experiencing strange behavior and I'm wondering if this is a bug or if I'm missing something.

The following code:

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)

fails with the following Exception:

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

If I remove the "aka" from the custom matcher everything works.

Thoughts?

Thanks Netta


Solution

  • You cannot use aka like this because you are effectively trying to assert that an Expectation, the object you create with aka is an instance of Zoo.

    If you want to specify a different failure message on a matcher you can write this:

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