2014-08-29 72 views
1

我試圖執行一個規範與多個測試,所有測試都運行在同一個Play應用程序,而不是一個單獨的應用程序爲每個測試。Specs2步驟不按順序執行

因此我有以下的代碼應該打印:

Play app started 
[info] PlayRunningImmutableSpec 
[info]  
[info]  + 200 status expected 
[info]  
[info]  + 404 status expected 
Play app stopped 

而是打印:

Play app started 
Play app stopped 
[info] PlayRunningImmutableSpec 
[info]  
[info] 
[info]  ! 200 status expected 
[error] ConnectException: : Connection refused: /127.0.0.1:19001 to http://127.0.0.1:19001/ 

我使用類型安全的激活1.2.10,其中包括播放2.3.3和2.3 Specs2 .12

下面的代碼有什麼問題,而代之以什麼?

import org.specs2.Specification 
import org.specs2.execute.Result 
import org.specs2.specification.Step 
import org.specs2.time.NoTimeConversions 
import play.api.Play 
import play.api.Play.current 
import play.api.http.{HeaderNames, HttpProtocol, Status} 
import play.api.libs.ws.WS 
import play.api.test._ 

class PlayRunningImmutableSpec extends Specification with NoTimeConversions with PlayRunners with HeaderNames with Status with HttpProtocol with DefaultAwaitTimeout with ResultExtractors with Writeables with RouteInvokers with FutureAwaits { 

    override def is = s2""" 
    ${Step(beforeAll)} 

    200 status expected  $e1 

    404 status expected  $e2 

    ${Step(afterAll)} 
    """ 

    def e1: Result = { 
    await(WS.url(s"http://127.0.0.1:${Helpers.testServerPort}").get()).status === 200 
    } 

    def e2: Result = { 
    await(WS.url(s"http://127.0.0.1:${Helpers.testServerPort}/missing").get()).status === 404 
    } 

    lazy val app = FakeApplication() 

    private def beforeAll = { 
    Play.start(app) 
    println("Play app started") 
    } 

    private def afterAll = { 
    Play.stop() 
    println("Play app stopped") 
    } 
} 

編輯:

我意識到我的錯誤是在利用play.api.Play.start方法,現在有一個簡單的特點來處理一個啓動和關閉:

trait PlayServerRunning extends SpecificationLike { 

    override def map(fs: => Fragments): Fragments = Step(beforeAll)^fs^Step(afterAll) 

    private lazy val server = TestServer(Helpers.testServerPort) 

    private def beforeAll = { 
    server.start() 
    } 

    private def afterAll = { 
    server.stop() 
    } 

} 

回答

2

這是在求婚。測試是並行執行的(根據執行上下文實現細節)。

如果您的測試需要按順序進行,則必須按照這種方式進行註釋。例如: -

"X" should { 
    sequential 

    "exp1" in { ... } 
    "exp2" in { ... } 
} 
+0

這並不爲任何一個可變的或不可變的規範工作 - 控制檯輸出不變(即:拒絕連接) – Cory 2014-08-29 13:39:38

+0

輸出是一回事,但未必反映執行順序。你在哪裏投入順序? – cchantep 2014-08-29 13:48:56

+0

不錯的一點,但是我在插入「s2」字符串內的各行之前在「override def is」以及「$ {sequential}」之前嘗試了它。上面的代碼片段可以用來證明這一點。 – Cory 2014-08-29 13:52:43