Still can't run multiple tests against play FakeApp with Salat / Casbah

有些话、适合烂在心里 提交于 2019-12-04 14:46:56

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

I've changed the implementation of how play-salat closes it's connections.

Usually it's a good thing closing down all the connections when the app stops, because the plugin will be re instantiated when the app starts again.

The problem was I was creating the mongodb connection in a lazy val which created the connection once, and when the app stopped it simply calls .close() on it.

What I've done to fix it is that the connection is now closed and if you ask for the connection again it creates a new one and passes it to you.

This is all available in the 1.1-SNAPSHOT version of play-salat which you can use straight away by adding

resolvers += "OSS Snapshots" at "https://oss.sonatype.org/content/repositories/snapshots/"

This release also includes salat 1.9.1 and support for capped collections and gridfs :)

Try it out and give me a shout if anything doesn't work as expected.

If everything looks good, I'll release the final 1.1 soon.

There are 2 phases in executing a specs2 Specification: first the specification is created, with all its examples. Then it is executed.

What you should do instead is:

class ModelSpec extends Specification with AroundExamples {
  // note the use of sequential here which is simpler than
  // overriding the "is" method
  sequential

  object FakeApp extends FakeApplication()

  // this method, defined in the AroundExamples trait
  // makes sure that every example is executed "inside"
  // the fake app.
  def around[R <% Result](r: =>R) = running(FakeApp)(r)

  // this will be only executed after the whole spec is created
  // if anything fails here, an exception will be caught, reported
  // and the rest will not execute
  step { 
    running(FakeApp) {
      println("set up database")
      val newUser = User(
                email = "wfbarksdale@gmail.com",
                username = "weezybizzle",
                password = "nutterbutter")
      User.save(newUser) 
    }
  }

  "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
      }
    }
  }
}

I hope that helps.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!