我在使用Scala 2.10.2(运行Java 1.7.0_51)构建的play 2.2.1中使用Specs2。我一直在阅读如何使用Specs2进行安装/拆卸。我看过使用“After”特征的例子,如下所示:如何在使用“in new WithApplication”时在specs2中进行setup/teardown
class Specs2Play extends org.specs2.mutable.Specification {
"this is the first example" in new SetupAndTeardownPasswordAccount {
println("testing")
}
}
trait SetupAndTeardownPasswordAccount extends org.specs2.mutable.After {
println("setup")
def after = println("teardown ")
}
这很好,除了我所有的测试都使用“in new WithApplication”。看来我需要的是一个既有“WithApplication”又有“After”的对象。下面无法编译,但本质上是什么,我想:
trait SetupAndTeardownPasswordAccount extends org.specs2.mutable.After with WithApplication
所以,我的问题是,如何做我安装/拆卸添加到其已经“在WithApplication”用我的测试?我主要关心的是,我们所有的测试都使用这样的假路由(所以他们需要With Application)。
val aFakeRequest = FakeRequest(method, url).withHeaders(headers).withBody(jsonBody)
val Some(result) = play.api.test.Helpers.route(aFakeRequest)
result
我喜欢你的建议,但它不完全工作。问题是在设置中我们有一个保存数据的上下文。拆解需要访问该环境。我已经生成了用户标识等,它们只存储在安装上下文中。你可以改进它,以便设置/拆卸上下文相同吗? – thebiggestlebowski
您可以使用'context'变量并将规范运行为'isolated'(以便每个示例在它自己的规范类中作为'sequential'在运行时执行(这样每个示例就不会彼此踩在一起) 。否则,您需要使用其他解决方案来解决您的问题(通过@LimbSoup) – Eric