加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 综合聚焦 > 服务器 > 安全 > 正文

scala – 执行规范之前和之后的代码

发布时间:2020-12-16 09:13:51 所属栏目:安全 来源:网络整理
导读:我有简单的规范,其中有几种情况: class MySpec extends Specification { "Something" should { "case 1" in { ... } "case 2" in { ... } }} 现在我需要启动应用程序,运行所有的情况并关闭应用程序.启动/停止应用程序是耗时的,我不希望在每种情况下发生. 案
我有简单的规范,其中有几种情况:

class MySpec extends Specification {

  "Something" should {

    "case 1" in {
      ...
    }

    "case 2" in {
      ...
    }
  }
}

现在我需要启动应用程序,运行所有的情况并关闭应用程序.启动/停止应用程序是耗时的,我不希望在每种情况下发生.

案件开始之前,所有案件完成后,如何运行代码?

解决方法

我已经提出了基于cmbaxter答案的以下解决方案.

import org.specs2.specification.Step

trait BeforeAllAfterAll extends Specification {
  // see http://bit.ly/11I9kFM (specs2 User Guide)
  override def map(fragments: =>Fragments) = 
    Step(beforeAll) ^ fragments ^ Step(afterAll)

  protected def beforeAll()
  protected def afterAll()
}

然后在BeforeAllAllAll之前混合规范并实现beforeAll和afterAll方法之间:

class MySpec extends Specification with BeforeAllAfterAll {

  def beforeAll() {
    println("Doing setup work...")
  }

  def afterAll() {
    println("Doing shutdown work...")
  }

  "Something" should {

    "case 1" in {
      ...
    }

    "case 2" in {
      ...
    }
  }
}

最后,提取初始化以分享规范:

trait InApplication extends BeforeAllAfterAll {
  def beforeAll() {
    println("Doing setup work...")
  }

  def afterAll() {
    println("Doing shutdown work...")
  }
}

class MySpec extends Specification with InApplication {

  "Something" should {

    "case 1" in {
      ...
    }

    "case 2" in {
      ...
    }
  }
}

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读