How to use "eventually" with "Specs2RouteTest"

537 views Asked by At

There are two specs here. First is not passing because eventually in check will not cause whole route rerun but this is the way I would prefer to follow. The second spec is the best solution I found (and prove that it is doable ;) ) but it contains some boilerplate like additional function which in real life will have to return rather tuple than single thing and it is inconsistent with spray-tests syntax design to test routs.

So question is: How to use eventually with spray-tests to be as close as possible to syntax from first spec.

import org.specs2.mutable.Specification
import spray.routing.Directives
import spray.http._
import MediaTypes._
import HttpCharsets._
import spray.testkit.Specs2RouteTest

class EventuallyAndRouts extends Specification with Directives with Specs2RouteTest {

    var i = 0
    def incAndGet = {
        i = i + 1
        println(s"This is i = $i")
        s"$i"
    }

    "The testing infrastructure should support an eventually matcher" >> {
        "but it is not working inside a check as I need :( (and this will fail)" in {
            i = 0
            Get() ~> complete(incAndGet) ~> check {
                body must eventually(7, 20 millis)(be_===(HttpEntity(ContentType(`text/plain`, `UTF-8`), "5")))
            }
        }
        "so I got workaround :/ (and this is passing)" in {
            i = 0
            def requestResult = Get() ~> complete(incAndGet) ~> check {
                body
            }
            requestResult must eventually(7, 20 millis)(be_===(HttpEntity(ContentType(`text/plain`, `UTF-8`), "5")))
        }
    }

}
1

There are 1 answers

2
Eric On

eventually is used to evaluate repeatedly a value that is changing. So it either has to be a var, a def or a byname parameter.

The best way around that is probably for you to implement a checkEventually(times, delay) method that will incorporate the eventually call. Something like that:

implicit class Checked(request: =>Request) {
  def checkEventually[R : AsResult](times: Int, delay: Duration, matcher: Matcher[RequestResult])(body: =>Unit)( = {
    val requestResult = result ~> check(body)
    requestResult must eventually(times, delay)(matcher)
  }
}