2014-08-29 68 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