仍然无法通过Salat/Casbah对Play FakeApp进行多次测试 [英] Still can't run multiple tests against play FakeApp with Salat / Casbah

查看:108
本文介绍了仍然无法通过Salat/Casbah对Play FakeApp进行多次测试的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我认为我已经修复了问题是暂时的,但事实证明我仍然有问题.

I thought I had fixed the problem temporarily, but it turns out I am still having problems.

我正在尝试为我的模型层创建一些specs2测试,我想插入一些虚拟对象,然后运行一些查询以确保数据按预期输出.这是我的简单测试的样子:

I am trying to create some specs2 tests for my models layer, I would like to insert some dummy object, and then run some queries to make sure data comes out as expected. Here is what my simple test looks like:


class ModelSpec extends Specification {

    override def is = args(sequential = true) ^ super.is

    object FakeApp extends FakeApplication()

    running(FakeApp){
        println("set up database")
        val newUser = User(
                    email = "wfbarksdale@gmail.com",
                    username = "weezybizzle",
                    password = "nutterbutter")
        User.save(newUser)
    }

    running(FakeApp) {
        "User Model" should {
            "be created and retrieved by username" in {
                println("finding someone")
                User.findOneByUsername("weezybizzle") must beSome
            }
            "not find non existant user" in {
                println("finding nobody")
                User.findOneByUsername("nobody") must beNone
            }
        }
    }
}

这是我从单元测试中获得的堆栈跟踪:

And here is the stack trace I get from the unit tests:


[info] Compiling 1 Scala source to /www/mojulo3/target/scala-2.9.1/test-classes...
set up database
finding someone
finding nobody
[info] ModelSpec
[info] 
[info] User Model should
[error] ! be created and retrieved by username
[error]     IllegalStateException: this Mongo has been closed (DBTCPConnector.java:123)
[error] com.mongodb.DBTCPConnector._checkClosed(DBTCPConnector.java:123)
[error] com.mongodb.DBTCPConnector.call(DBTCPConnector.java:207)
[error] com.mongodb.DBApiLayer$MyCollection.__find(DBApiLayer.java:313)
[error] com.mongodb.DBApiLayer$MyCollection.__find(DBApiLayer.java:298)
[error] com.mongodb.DBCollection.findOne(DBCollection.java:682)
[error] com.mongodb.DBCollection.findOne(DBCollection.java:661)
[error] com.mongodb.casbah.MongoCollectionBase$class.findOne(MongoCollection.scala:225)
[error] com.mongodb.casbah.MongoCollection.findOne(MongoCollection.scala:897)
[error] com.novus.salat.dao.SalatDAO.findOne(SalatDAO.scala:311)
[error] models.User$.findOneByUsername(User.scala:24)
[error] test.ModelSpec$$anonfun$3$$anonfun$apply$1$$anonfun$apply$2$$anonfun$apply$3.apply(ModelSpec.scala:29)
[error] test.ModelSpec$$anonfun$3$$anonfun$apply$1$$anonfun$apply$2$$anonfun$apply$3.apply(ModelSpec.scala:29)
[error] test.ModelSpec$$anonfun$3$$anonfun$apply$1$$anonfun$apply$2.apply(ModelSpec.scala:29)
[error] test.ModelSpec$$anonfun$3$$anonfun$apply$1$$anonfun$apply$2.apply(ModelSpec.scala:27)
[error] ! not find non existant user
[error]     IllegalStateException: this Mongo has been closed (DBTCPConnector.java:123)
[error] com.mongodb.DBTCPConnector._checkClosed(DBTCPConnector.java:123)
[error] com.mongodb.DBTCPConnector.call(DBTCPConnector.java:207)
[error] com.mongodb.DBApiLayer$MyCollection.__find(DBApiLayer.java:313)
[error] com.mongodb.DBApiLayer$MyCollection.__find(DBApiLayer.java:298)
[error] com.mongodb.DBCollection.findOne(DBCollection.java:682)
[error] com.mongodb.DBCollection.findOne(DBCollection.java:661)
[error] com.mongodb.casbah.MongoCollectionBase$class.findOne(MongoCollection.scala:225)
[error] com.mongodb.casbah.MongoCollection.findOne(MongoCollection.scala:897)
[error] com.novus.salat.dao.SalatDAO.findOne(SalatDAO.scala:311)
[error] models.User$.findOneByUsername(User.scala:24)
[error] test.ModelSpec$$anonfun$3$$anonfun$apply$1$$anonfun$apply$6$$anonfun$apply$7.apply(ModelSpec.scala:35)
[error] test.ModelSpec$$anonfun$3$$anonfun$apply$1$$anonfun$apply$6$$anonfun$apply$7.apply(ModelSpec.scala:35)
[error] test.ModelSpec$$anonfun$3$$anonfun$apply$1$$anonfun$apply$6.apply(ModelSpec.scala:35)
[error] test.ModelSpec$$anonfun$3$$anonfun$apply$1$$anonfun$apply$6.apply(ModelSpec.scala:33)
[info]  
[info]  
[info] Total for specification ModelSpec
[info] Finished in 20 ms
[info] 2 examples, 0 failure, 2 errors
[info] 
[error] Error: Total 2, Failed 0, Errors 2, Passed 0, Skipped 0
[error] Error during tests:
[error]     test.ModelSpec
[error] {file:/www/mojulo3/}mojulo3/test:test: Tests unsuccessful
[error] Total time: 4 s, completed Aug 28, 2012 10:02:33 PM

看起来FakeApp正在以某种方式从数据库断开连接,而不是重新连接.我查看了Play源代码,看来该应用程序将停止并重新启动,无论它实际上是否是同一对象.我认为该问题可能是由于礼拜法onStop()方法或onStart()引起的,但我不确定.

It looks like the FakeApp is somehow disconnecting from the database, and not reconnecting. I looked into the Play source, and it seems that the application will be stopped and restarted, regardless of whether or not it is actually the same object. I am thinking that the issue may be stemming from the salat onStop() method or onStart() but I am not really sure.

我已经为此苦苦挣扎了一段时间,对您的任何帮助,甚至只是关于如何解决问题的帮助,都将不胜感激.

I have been struggling for a while with this, any help, even just on how to reason about the problem, would be greatly appreciated.

推荐答案

这是因为它在插件的onStop方法中关闭了MongoDB连接.我已提交拉取请求,以便在测试期间不会发生这种情况:

This is because it closes the MongoDB connection in the plugin's onStop method. I've submitted a pull request so that this doesn't happen during testing:

https://github.com/leon/play-salat/pull/27

这篇关于仍然无法通过Salat/Casbah对Play FakeApp进行多次测试的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆