【问题标题】:Execute specs2 examples wrapped in function执行包含在函数中的 specs2 示例
【发布时间】:2012-04-19 20:19:53
【问题描述】:

如何在包装函数中执行 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")
      }
    }
  }

所以,这 3 个测试中的每一个都应该在

with(someSession){...

使用 ScalaTest 时,我可以覆盖 withFixture

【问题讨论】:

    标签: unit-testing scala specs2


    【解决方案1】:

    你可以使用类似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)
      }
      ...
    }
    

    根据您需要执行的具体操作,BeforeBeforeAfterOutside 上下文(及其对应的 Example)可能更适合。

    【讨论】:

      猜你喜欢
      • 2021-05-06
      • 2019-06-22
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-09-05
      • 2012-11-09
      • 2018-08-23
      • 1970-01-01
      相关资源
      最近更新 更多