2016-11-16 26 views
0

我試圖使用GWT規格完全支持,但其official documentation的示例有點簡單。寫入給定/然後/當具有複雜對象的規格

在搜索SO,我發現這樣一個問題:

但它太舊(3年),我認爲在specs2GWT的方式發生了變化。

到目前爲止,我有這個簡單的測試:

class FlowCollectorSpec extends Specification 
    with GWT 
    with StandardRegexStepParsers { def is = s2""" 

Given a API route to get flows            ${apiCaller.start} 
    Given an access to the API URL: http://192.168.56.102:8080/stats/flow/ 
    When getting flow stats for a switch with id: 1 
    Then status code should be: 200           ${apiCaller.end} 
""" 

    val anAPIUri = readAs(".*: (.*)$").and((s: String) => s) 

    val apiCaller = 
    Scenario("apiCaller"). 
     given(aString). 
     given(anInt). 
     when(anAPIUri) {case url :: dpid :: _ => FlowCollector.getSwitchFlows(dpid)}. 
     andThen(anInt) {case expected :: actual :: _ => actual.code must_== expected} 
} 

我怎麼可以指定在鑑於聲明一個複雜的對象?是這樣的:

Given a Json response: ${jsonResponse} 

回答

1

如果你的數據很複雜並且不能在一行上顯示,你可以簡單地寫

class FlowCollectorSpec extends Specification 
    with GWT 
    with StandardRegexStepParsers { def is = s2""" 

Given a API route to get flows             ${apiCaller.start} 
    Given an access to the API URL:  http://192.168.56.102:8080/stats/flow/ 
    Given some complex data 
    When getting flow stats for a switch with id: 1 
    Then status code should be: 200            ${apiCaller.end} 
""" 

    val anAPIUri = readAs(".*: (.*)$").and((s: String) => s) 

    val apiCaller = 
    Scenario("apiCaller"). 
     given(aString). 
     given(complexData). 
     given(anInt). 
     when(anAPIUri) { case url :: Json(j) :: dpid :: _ => FlowCollector.getSwitchFlows(dpid) }. 
     andThen(anInt) { case expected :: actual :: _ => actual.code must_== expected } 

    val complexData = readAs(".*").andThen(_ => Json("some json")) 

    case class Json(value: String) 

    object FlowCollector { 
    def getSwitchFlows(dpid: Int) = FlowResult(code = 200) 
    } 

    case class FlowResult(code: Int) 
} 

否則你的解決方案是正確的。

+0

可能是您的解決方案更好的結構。我會嘗試它。 「FlowCollector」對象和「FlowResult」的目的是什麼? – elbaulp

+0

我添加了'FlowCollector'來使你的原始代碼編譯和'FlowResult'重現一個有'.code'方法的東西(再次使代碼編譯)。 – Eric

+0

啊,好的,謝謝。你認爲什麼樣的結構化測試更好? – elbaulp

0

我終於想到了一個解決方案,我不知道這是否是做的正確的方式,但現在它正在努力:

class FlowCollectorSpec extends Specification 
    with GWT 
    with StandardRegexStepParsers { def is = s2""" 

Given a API route to get flows            ${connectTest.start} 
    Given an access to the API URL: http://192.168.56.102:8080/stats/flow/ 
    When getting flow stats for a switch with id: 1 
    Then status code should be: 200           ${connectTest.end} 

Retrieving values               ${gettingValues.start} 
    Given an api call response: {"1": [{"actions": ["OUTPUT:CONTROLLER"], "idle_timeout": 0, "cookie": 0, "packet_count": 1212, "hard_timeout": 0, "byte_count": 72720, "duration_sec": 432, "duration_nsec": 903000000, "priority": 65535, "length": 96, "flags": 0, "table_id": 0, "match": {"dl_type": 35020, "dl_dst": "01:80:c2:00:00:0e"}}, {"actions": ["OUTPUT:CONTROLLER"], "idle_timeout": 0, "cookie": 0, "packet_count": 49, "hard_timeout": 0, "byte_count": 3890, "duration_sec": 432, "duration_nsec": 938000000, "priority": 0, "length": 80, "flags": 0, "table_id": 0, "match": {}}]} 
    When extracting key: packet_count 
    Then a field look up should return: true         ${gettingValues.end} 
""" 

    val stepParser = readAs(".*: (.*)$").and((s: String) => s) 
    val jsonExtractor = readAs(".+?: (.*)").and((s:String) => JsonParser(s).asJsObject) 
    val aJsonKey = aString 

    val connectTest = 
    Scenario("connectTest"). 
     given(aString). 
     given(anInt). 
     when(stepParser) {case url :: dpid :: _ => FlowCollector.getSwitchFlows(dpid)}. 
     andThen(anInt) {case expected :: actual :: _ => actual.code must_== expected} 

    val gettingValues = 
    Scenario("Getting Values"). 
     given(jsonExtractor). 
     when(aJsonKey) { case key :: json :: _ => json.fields contains key}. 
     andThen(){ 
     case expected :: exists :: _ => 
      if (expected == "true") exists must_== true 
      else exists must_==false 
     } 
}