好的,正如我在評論中提到的那樣,我實際上也有同樣的問題。我需要測試未經過濾的端點,每種規範的最佳方法是使用單個端點啓動未經過濾的服務器,運行規範然後關閉服務器。爲了實現這一點,我首先定義一個類似的基本規格:
import org.specs2.mutable.Specification
abstract class SpecificationBase extends Specification{
//Do setup work here
step{
println("Doing setup work...")
success
}
//Include the real spec from the derived class
include(spec)
//Do shutdown work here
step{
println("Doing shutdown work...")
success
}
/**
* To be implemented in the derived class. Returns the real specification
* @return Specification
*/
def spec:Specification
}
基本上,這個基類組裝完整的規範作爲設置步驟和拆卸步驟與真實規範(在具體的規範類中定義)夾在中間。因此,使用這個基類的測試是這樣的:
class MySpec extends SpecificationBase{ def spec =
new Specification{
"A request to do something" should{
"be successful in case 1" in {
println("Testing case 1")
success
}
"be successful in case 2" in {
println("Testing case 2")
success
}
}
}
}
當你運行它,你會看到:
Doing setup work...
Testing case 1
Testing case 2
Doing shutdown work...
它並不完美,但它的作品。是否有另一種(和可能更清潔/更好)的方式來做到這一點?可能,但這是您可以考慮使用的一種解決方案。
[向下滾動,直到在此之前,文檔頁/後段(http://etorreborre.github.io/specs2/guide/org.specs2 .guide.Structure.html) –
@ om-nom-nom它只解釋瞭如何執行每個案例的代碼。 – lambdas
哎呀,我的意思是'Template'或'Global setup/teardown' paragraph(帶有數據庫設置/清理的那個):-) –