在play 2.0 scala中的同一个FakeApplication()中运行多个测试

wfb*_*ale 14 unit-testing scala specs2 playframework-2.0

我正在尝试学习Play scala中的单元测试,但我遇到了一些问题.我试图在我的模型层上运行几个测试,如下所示:

"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
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

但是,当以这种方式执行操作时,我无法在第二个单元测试中连接到数据库,说连接已关闭.我试图通过将所有代码包含在同一个假应用程序上运行的块中来解决这个问题,但这也没有用.

  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
    }
  }
Run Code Online (Sandbox Code Playgroud)

Raj*_*ish 14

默认情况下,specs2测试是并行执行的,这可能会导致访问数据库时出现问题,尤其是当您依赖先前测试提供的db内容时.因此,要强制进行顺序测试,您必须告诉specs2这样做:

class ModelSpec extends Specification with Logging {
  override def is = args(sequential = true) ^ super.is
...
}
Run Code Online (Sandbox Code Playgroud)

对于在一个中完成的测试,FakeApplication您可以将整个测试包装在其中:

  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")
      }
Run Code Online (Sandbox Code Playgroud)

整个样本可以在这里找到.这是我用Play测试MongoDB时第一次尝试处理问题!框架.

我从salat项目借来的第二种方法,这是处理MongoDB的一个非常好的规范示例来源(尽管它不是Play!框架应用程序).你必须定义一个特征延伸AroundScope,在那里你可以把任何你需要在一个应用实例进行初始化:

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
  }
}
Run Code Online (Sandbox Code Playgroud)

编辑2013-06-30:

在当前版本specs2around签名应该是:

def around[T : AsResult](test: => T): Result
Run Code Online (Sandbox Code Playgroud)

编辑结束

然后可以这样写一个测试:

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
      }
    }
  }
}
Run Code Online (Sandbox Code Playgroud)

可在此处找到此类套件的完整示例.

最后说明:在启动套件之前清理测试数据库也是很好的:

  step {
    MongoConnection().dropDatabase("test_db")
  }
Run Code Online (Sandbox Code Playgroud)