Michael Lafayette
Michael Lafayette

Reputation: 3072

Play framework specs2 weird matcher syntax

In Play framework specs2 testing I have these lines...

    new WithApplication {
      val homeResponse = route(FakeRequest(GET, "/")).get
      val resultType = contentType(homeResponse)
      resultType.must( beSome.which( _ == "text/html") )
    }

^ This works, but when I pull " beSome.which( _ == "text/html") " into a separate variable...

    new WithApplication {
      val homeResponse = route(FakeRequest(GET, "/")).get
      val resultType = contentType(homeResponse)
      val textTypeMatcher = beSome.which( _ == "text/html")
      resultType.must( textTypeMatcher )
    }

^ Type Mismatch.

expected: Matcher[Option[String]], actual: OptionLikeCheckedMatcher[Option, Nothing, Nothing] ^

What is going on here?

Upvotes: 0

Views: 91

Answers (1)

kornfridge
kornfridge

Reputation: 5200

In the first case, it infers the type based on resultType.must(, because resultType is a String. But when you split it, there's nothing to infer from, so you'll get:

val textTypeMatcher = beSome.which( _ == "text/html")
=> OptionLikeCheckedMatcher[Option, Nothing, Nothing]

But if you add in the type: beSome[String], it you'll get the correct type again.

val textTypeMatcher = beSome[String].which( _ == "text/html")
=> OptionLikeCheckedMatcher[Option, String, String]

Edit:

You can also do this:

val textTypeMatcher: OptionLikeCheckedMatcher[Option, String, String] = beSome.which( _ == "text/html")

So basically, if you give the type inferrer something to work with, it will infer that _ must be a string. Otherwise, there is no way for Scala to know that _ is supposed to be a string.

Upvotes: 3

Related Questions