4

ラッパー関数で 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をオーバーライドして実行できます。

4

1 に答える 1

7

次のようなものを使用できますAroundExample

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

または暗黙のコンテキスト オブジェクト:

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

正確に何をする必要があるかに応じて、BeforeBeforeAfter、またはOutsideコンテキスト (およびそれらのExample対応するもの) の組み合わせがより適切になる場合があります。

于 2012-04-18T20:50:51.190 に答える