0

这里有两个规格。首先是不通过,因为eventuallyincheck不会导致整个路线重新运行,但这是我更愿意遵循的方式。第二个规范是我找到的最好的解决方案(并证明它是可行的;))但它包含一些样板,如附加函数,在现实生活中必须返回元组而不是单一的东西,它与喷雾测试语法设计不一致测试路线。

所以问题是: 如何使用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")))
        }
    }

}
4

1 回答 1

0

eventually用于重复评估一个正在变化的值。所以它必须是 a var、 adef或 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)
  }
}
于 2014-11-26T22:19:49.940 回答