2016-11-16 1 views
0

私はGWTの仕様を全面的にサポートしようとしていますが、official documentationの例はちょっと単純です。 SOでの検索複雑なオブジェクトを指定してGiven/Then/Specを書くとき

私はこの質問が見つかりました:

をしかし、それは(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

あなたのデータが複雑な場合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'を追加し、(コードをコンパイルするために)' .code'メソッドを持つものを再現するために 'FlowResult'を追加しました。 – Eric

+0

Ahh、ありがとう、ありがとう。テストをどのように構造化すれば良いと思いますか? – 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 
     } 
} 
関連する問題