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

前端之家收集整理的这篇文章主要介绍了unit-testing – 执行包含在函数中的specs2示例前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
如何在包装函数中执行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上下文(及其示例对应项)的某些组合可能更适合.

猜你在找的Scala相关文章