在play 2.0 scala的同一FakeApplication()中运行多个测试 [英] running multiple tests within the same FakeApplication() in play 2.0 scala
问题描述
我正在尝试学习Play scala中的单元测试,但是遇到了一些问题.我正在尝试在我的模型层上运行几个测试,如下所示:
I am trying to learn the unit tests in Play scala, but I am running into some issues. I am trying to run several tests on my models layer like this:
"User Model" should {
"be created and retrieved by username" in {
running(FakeApplication()) {
val newUser = User(username = "weezybizzle",password = "password")
User.save(newUser)
User.findOneByUsername("weezybizzle") must beSome
}
}
"another test" in {
running(FakeApplication()) {
// more tests involving adding and removing users
}
}
}
但是,当以这种方式执行操作时,我无法在第二个单元测试中连接到数据库,并说该连接已关闭.我试图通过将所有代码封装在同一假应用程序上运行的代码块中来解决此问题,但这也不起作用.
However when doing things this way, I fail to connect to the database on the second unit test, saying that the connection is closed. I tried to solve this by enclosing all the code in a block that runs on the same fake application, but that didn't work either.
running(FakeApplication()) {
"be created and retrieved by username" in {
val newUser = User(username = "weezybizzle",password = "password")
User.save(newUser)
User.findOneByUsername("weezybizzle") must beSome
}
"another test" in {
// more tests involving adding and removing users
}
}
推荐答案
默认情况下,specs2测试是并行执行的,这可能会导致访问数据库时出现问题,尤其是当您依赖于先前测试提供的db内容时.因此,要强制进行顺序测试,您必须告诉specs2这样做:
The specs2 tests are performed by default in parallel which may cause problems with accessing databases, especially when you rely on the db contents provided by a previous test. So to force sequential testing you have to tell specs2 to do so:
class ModelSpec extends Specification with Logging {
override def is = args(sequential = true) ^ super.is
...
}
对于在一个FakeApplication
中完成的测试,您可以将整个测试包装在其中:
For tests done in one FakeApplication
you can wrap the whole tests in it:
running(FakeApp) {
log.trace("Project tests.")
val Some(project) = Project.findByName("test1")
"Project" should {
"be retrieved by name" in {
project must beAnInstanceOf[Project]
project.description must endWith("project")
}
可以在此处一个>.这是我在Play上测试MongoDB时第一次尝试解决问题!框架.
The whole sample can be found here. That was my first attempt to deal with problems while testing MongoDB with Play! framework.
我从 salat 项目中借用的第二种方法一个很好的有关MongoDB的规范示例的来源(尽管它不是Play!框架应用程序).您必须定义一个扩展Around
和Scope
的特征,您可以在其中将需要初始化的任何内容放在应用程序实例中:
The second approach I borrowed from the salat project, which is by the way a very good source of specs examples dealing with MongoDB (although it is not a Play! framework app). You have to define a trait extending Around
and Scope
, where you can put anything you need to be initialized in an application instance:
import org.specs2.mutable._
import org.specs2.execute.StandardResults
import play.api.mvc._
import play.api.mvc.Results
import play.api.test._
import play.api.test.Helpers._
trait FakeApp extends Around with org.specs2.specification.Scope {
val appCfg = Map(
"first.config.key" -> "a_value",
"second.config.key" -> "another value"
)
object FakeApp extends FakeApplication(
additionalPlugins = Seq("com.github.rajish.deadrope.DeadropePlugin"),
additionalConfiguration = appCfg
) {
// override val routes = Some(Routes)
}
def around[T <% org.specs2.execute.Result](test: => T) = running(FakeApp) {
Logger.debug("Running test ==================================")
test // run tests inside a fake application
}
}
编辑2013-06-30:
在当前版本的specs2
中,around
签名应为:
In the current version of specs2
the around
signature should be:
def around[T : AsResult](test: => T): Result
编辑结束
然后可以这样编写一个测试:
Then a test can be written like that:
class SomeSpec extends Specification { sequential // according to @Eric comment
"A test group" should {
"pass some tests" in new FakeApp {
1 must_== 1
}
"and these sub-tests too" in {
"first subtest" in new FakeApp {
success
}
"second subtest" in new FakeApp {
failure
}
}
}
}
On a final note: It's also good to clean up the test database before starting a suite:
step {
MongoConnection().dropDatabase("test_db")
}
这篇关于在play 2.0 scala的同一FakeApplication()中运行多个测试的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!