2012-04-18 35 views
4

如何在包裝函數中執行spec2規範的所有測試?執行包裝在函數中的specs2示例

例如:

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個測試應在

與(someSession){...裏面執行

當使用ScalaTest,我可以做,覆蓋與固定

回答

7

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

取決於你需要做什麼,在BeforeBeforeAfter,或Outside環境的某種組合(和他們的Example同行)可能更合適。