I'm new to the Scala/Play 2.1/Specs2 stack, so please excuse me if this question seems simpleton, but I am having difficulty writing a spec to test the case of "String contains the word 'GET'". I've got a Play 2.1 Action that returns an Access-Control-Allow-Methods header value like
Access-Control-Allow-Methods: GET, PUT, POST, DELETE, OPTIONS
My Spec has no problem doing straight equality checks on other headers, but I have been unable to figure out how to check the Access-Control-Allow-Methods header for each of GET, PUT, POST, DELETE, and OPTIONS. I've expected something like "must contains("GET") to work but the IDE goes red on this with some:
type mismatch; found : org.specs2.matcher.ContainMatcher[String] required: org.specs2.matcher.Matcher[Option[String]] SessionsSpec.scala /dm2-server/test/admin line 53 Scala Problem
My spec looks like...
"send 200 on OPTIONS request on valid route" in {
running(FakeApplication()) {
var fakeReq = FakeRequest("OPTIONS", "/admin/sessions")
val Some(result) = route(fakeReq)
status(result) must equalTo(OK)
header(ACCESS_CONTROL_ALLOW_ORIGIN, result) must equalTo(Some("*"))
header(ACCESS_CONTROL_ALLOW_HEADERS, result) must equalTo(Some(CONTENT_TYPE))
val expectedMethods = Seq(GET, PUT, POST, DELETE, "OPTIONS")
header(ACCESS_CONTROL_ALLOW_METHODS, result) must containAllOf(expectedMethods)
}
}
How do I express the use case of "does this string contain all of these values" in Specs2?
Here's something similar to what you are trying to accomplish:
"A split option string " should{
"be able to be verified with a containsAllOf matcher" in {
val headerVal = Some("a, b, c, d, e")
val expected = Seq("a", "e")
headerVal must beSome
headerVal.get.split(", ").toSeq must containAllOf(expected)
}
}
The issue was that you were trying to take an Option[String]
and use that in a Traversable
matcher against a Seq
. To make that kind of comparison work, both items need to be Traversable
. To get to that point, I first make a check on the Option
to make sure it's a Some
. I then get it and split it on the comma and turn that into a Seq
. Once both pieces are Traversable
, you can use that containAllOf
matcher.