单元测试 – 在play 2.0 scala中,在同一个FakeApplication()中运
发布时间:2020-12-16 09:20:06 所属栏目:安全 来源:网络整理
导读:我正在尝试在Play scala中学习单元测试,但是我遇到了一些问题.我试图在我的模型层上运行几个测试,如下所示: "User Model" should { "be created and retrieved by username" in { running(FakeApplication()) { val newUser = User(username = "weezybizzle
|
我正在尝试在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
}
}
}
但是,以这种方式进行操作时,我无法连接到第二单元测试中的数据库,表示连接已关闭.我试图通过将所有的代码封装在同一个假的应用程序上运行的一个程序块中来解决这个问题,但是这样也不行. 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测试默认并行执行,这可能会导致访问数据库的问题,特别是当您依赖前一测试提供的数据库内容时.所以要强制连续测试你必须告诉specs2这样做:
class ModelSpec extends Specification with Logging {
override def is = args(sequential = true) ^ super.is
...
}
对于在一个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")
}
整个示例可以找到here.这是我第一次尝试处理问题,同时测试MongoDB与Play!框架. 我从salat项目借来的第二种方法,这是一个很好的来源于MongoDB的规范例子(尽管它不是一个Play!框架应用程序).您必须定义一个在“周围”和“范围”中扩展的特征,您可以在应用程序实例中放置任何需要初始化的内容: 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中,周围的签名应为: def around[T : AsResult](test: => T): Result 编辑结束 然后可以这样写一个测试: 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
}
}
}
}
这个套件的完整样本可以在here找到. 最后一个注意事项:在启动套件之前清理测试数据库也很好: step {
MongoConnection().dropDatabase("test_db")
}
(编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |
相关内容
- twitter-bootstrap – Bootstrap 3中的旧非平面主题
- scala – 当关键字在不同的上下文中表示不同的东西时,这是一
- Scala和Aspects
- 当reloadOnSearch为false时,$location.search上的AngularJs
- 在scala中使用self =>进行混淆
- 分享bootstarp3.0.2响应式模板-第二弹[Metronic_conquer]
- dotnet build使用dotnet在docker容器中失败:2.1-sdk
- scala – 为什么这个电话隐含含糊不清?
- 关于angularjs $asyncValidators $http的异步验证扩展踩过的
- 检测键是否绑定到vim中的某个对象
