如何使用“Specs2RouteTest”“最终”

时间:2014-11-26 10:43:44

标签: scala scalatest spec2 spray-test

这里有两个规格。首先是没有通过,因为eventually中的check不会导致整个路线重新运行,但这是我希望遵循的方式。第二个规范是我找到的最好的解决方案(并证明它是可行的;))但它包含一些类似于附加功能的样板,在现实生活中必须返回比单个元素更多的元组并且它与喷涂测试语法设计不一致测试溃败。

所以问题是: 如何使用eventually喷涂测试尽可能接近第一个规范的语法。

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 个答案:

答案 0 :(得分:0)

eventually用于重复评估正在变化的值。因此它必须是vardef或byname参数。

最好的解决方法可能是您实施一个checkEventually(times, delay)方法,该方法将包含eventually调用。这样的事情:

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)
  }
}