วิธีการใช้งานในที่สุดด้วย Specs2RouteTest

มีข้อกำหนดสองประการที่นี่ อย่างแรกไม่ผ่านเพราะ 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")))
        }
    }

}

person panurg    schedule 26.11.2014    source แหล่งที่มา


คำตอบ (1)


eventually ใช้เพื่อประเมินค่าที่เปลี่ยนแปลงซ้ำๆ ดังนั้นจึงต้องเป็นพารามิเตอร์ var, def หรือชื่อจริง

วิธีที่ดีที่สุดคือให้คุณใช้วิธี 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)
  }
}
person Eric    schedule 26.11.2014
comment
ฉันได้เขียนสิ่งที่คล้ายกันตามข้อเสนอแนะของคุณ (ฉันได้ละเว้นการประกาศโดยนัยและคลาสและเพิ่มวิธีการให้กับคลาสข้อมูลจำเพาะ) และฉันสังเกตเห็นว่าการเรียก: result ~› check(body) จะไม่ทำให้เกิดการรันเส้นทางอีกครั้ง มันเป็นเพียงการตรวจสอบผลลัพธ์เดียวกันไม่กี่ครั้ง - person panurg; 27.11.2014
comment
ฉันไม่คุ้นเคยกับการกำหนดเส้นทางของสเปรย์ แต่ใช่ คุณต้องเรียกใช้ฟังก์ชันที่จะเรียกใช้คำขออีกครั้ง โปรดทราบว่าฉันกำลังใช้พารามิเตอร์ byname ในคลาสโดยนัยกับเอฟเฟกต์นั้น บางทีคุณควรถามรายชื่อผู้รับจดหมายสเปรย์ว่ามีหรือไม่? - person Eric; 28.11.2014