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

unit-testing – 执行包含在函数中的specs2示例

发布时间:2020-12-16 18:03:26 所属栏目:安全 来源:网络整理
导读:如何在包装函数中执行spec2规范的所有测试? 例如: class HelloWorldSpec extends Specification { wrapAll(example) = { // wrap it in a session,for example. with(someSession){ example() } } "The 'Hello world' string" should { "contain 11 charac
如何在包装函数中执行spec2规范的所有测试?

例如:

class HelloWorldSpec extends Specification {

    wrapAll(example) = {
        // wrap it in a session,for example.
        with(someSession){
            example()
        }
    }

    "The 'Hello world' string" should {
      "contain 11 characters" in {
        "Hello world" must have size(11)
      }
      "start with 'Hello'" in {
        "Hello world" must startWith("Hello")
      }
      "end with 'world'" in {
        "Hello world" must endWith("world")
      }
    }
  }

所以,这三个测试中的每一个都应该在内部执行

with(someSession){…

使用ScalaTest时,我可以用theFixture覆盖它

解决方法

你可以使用像 AroundExample这样的东西:

class HelloWorldSpec extends Specification with AroundExample {
  def around[T <% Result](t: =>T) = inWhateverSession(t)
  ...
}

或者是implicit context object:

class HelloWorldSpec extends Specification {
  implicit object sessionContext = new Around {
    def around[T <% Result](t: =>T) = inWhateverSession(t)
  }
  ...
}

根据您需要做什么,Before,BeforeAfter或Outside上下文(及其示例对应项)的某些组合可能更适合.

(编辑:李大同)

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

    推荐文章
      热点阅读