Skip to content

Instantly share code, notes, and snippets.

@mzkrelx
Created August 4, 2013 15:35
Show Gist options
  • Select an option

  • Save mzkrelx/6150705 to your computer and use it in GitHub Desktop.

Select an option

Save mzkrelx/6150705 to your computer and use it in GitHub Desktop.
specs2 の事前処理、事後処理
package views.admin.mlproposals
import org.specs2.mutable.After
import org.specs2.mutable.Specification
import org.specs2.specification.AfterExample
import org.specs2.specification.BeforeExample
import org.specs2.specification.Scope
import play.api.test.Helpers.HTMLUNIT
import play.api.test.Helpers.running
import play.api.test.TestServer
class IntegrationSpec extends Specification with BeforeExample with AfterExample {
def before = {
println("==== before ====")
}
def after = {
println("==== after ====")
}
"Application" should {
"work from within a browser" in new TestScope {
running(TestServer(3333), HTMLUNIT) { browser =>
browser.goTo("http://localhost:3333/")
browser.waitUntil[Boolean]{
browser.pageSource contains ("Hello")
}
}
}
}
"Application2" should {
"work from within a browser" in {
running(TestServer(3333), HTMLUNIT) { browser =>
browser.goTo("http://localhost:3333/")
browser.waitUntil[Boolean]{
browser.pageSource contains ("World")
}
}
}
}
trait TestScope extends Scope with After {
println("**** before ****")
def after = {
println("**** after ****")
}
}
}
@mzkrelx

mzkrelx commented Aug 4, 2013

Copy link
Copy Markdown
Author

play2 + scala で specs2 の事前処理、事後処理をしたいときのサンプル

TestScope は、個別にやりたいときだけ BeforeAfter したいときに in の後に書いて使う。
複数のScopeを定義して使い分けもできる感じ。

全テスト固定の処理でよい場合は BeforeExample と AfterExample をミックスインして before・after メソッドを定義する。

サンプルの実行結果

==== before ====
**** before ****
**** after ****
==== after ====
==== before ====
==== after ====

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment