【问题标题】:Specs2 Steps not executing in orderSpecs2 步骤未按顺序执行
【发布时间】:2014-08-30 06:55:57
【问题描述】:

我正在尝试执行包含多个测试的规范,这些测试都在同一个 Play 应用程序中运行,而不是每个测试的单独应用程序。

因此,我有以下应该打印的代码:

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/

我正在使用 Typesafe Activator 1.2.10,其中包括 Play 2.3.3 和 Specs2 2.3.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()
  }

}

【问题讨论】:

    标签: scala specs2


    【解决方案1】:

    这是提议。测试是并行执行的(根据执行上下文的实现细节)。

    如果您的测试需要按顺序进行,则必须以这种方式进行注释。例如:

    "X" should {
      sequential
    
      "exp1" in { ... }
      "exp2" in { ... }
    }
    

    【讨论】:

    • 这对于可变或不可变规范都不起作用 - 控制台输出未更改(即:连接被拒绝)
    • 输出是一回事,但可能不反映执行顺序。你把顺序放在哪里了?
    • 好点,但是我在插值“s2”字符串中的各个行上的“覆盖def is”和“$ {sequential}”之前的行上尝试了它。上面的代码sn-p可以证明这一点。
    猜你喜欢
    • 2015-08-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-09-15
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多