I am trying to test a POST spray-route with scalatest which require a mandatory parameter adId. And Cannot make it work. My code follows
import akka.actor._
import akka.event.LoggingReceive
import akka.testkit.{TestProbe}
import com.ss.rg.service.ad.AdImporterServiceActor.{GetImportStatus, StatusOfImport}
import org.scalatest.{MustMatchers, WordSpecLike}
import spray.http.{StatusCodes, MediaTypes}
import spray.testkit.ScalatestRouteTest
class AdServiceApiTest extends WordSpecLike with MustMatchers with ScalatestRouteTest{
"AdService REST api " must{
"POST for import witout mandatory parameters should fail with " in{
val p = TestProbe()
val addressServiceMock = system.actorOf(Props(classOf[AdServiceActorMock],p.ref))
Post("/service/ad/import") ~> new AdServiceApi(addressServiceMock).route ~>check{
handled must be(false)
status must be (StatusCodes.BadRequest)
}
}
}
The test fails but for a different reason
Request was rejected with List(MissingQueryParamRejection(adId))
org.scalatest.exceptions.TestFailedException: Request was rejected with List(MissingQueryParamRejection(adId))
at spray.testkit.ScalatestInterface$class.failTest(ScalatestInterface.scala:25)
at com.ss.rg.api.ad.AdServiceApiTest.failTest(AdServiceApiTest.scala:19)
at spray.testkit.RouteResultComponent$RouteResult$$anonfun$response$1$$anonfun$apply$1.apply(RouteResultComponent.scala:97)
at spray.testkit.RouteResultComponent$RouteResult$$anonfun$response$1$$anonfun$apply$1.apply(RouteResultComponent.scala:95)
at scala.Option.foreach(Option.scala:236)
at spray.testkit.RouteResultComponent$RouteResult$$anonfun$response$1.apply(RouteResultComponent.scala:94)
...
Seems that status hasn't been even checked. Second thing which is not fully cleared to me is how to actually set a adId parameter in spray-testkit? One way would be via setting the header but I wouldn't be surprised that a better way would exist.
Can somebody more experienced with spray-testkit comment?
Thx
There isn't a status - the route rejected the request. You can access the rejection as rejection
and assert that it's the type you expect. If you want to check what the browser would actually see you should wrap the route in a handleRejections
directive using the default RejectionHandler
(it's available implicitly) and then you'll be able to see the status code you'd expect. In that case handled
will come out as true
though (because the wrapped route will handle the request - by returning a response with a failed statuscode and an error message).