Hi, I'm not going to write here a complete step-by-step tutorial to test such applications. There should be already lot of them in the Internet. I want just present general way and solution to some problem, that might not be described yet.
First of all experience proved that its good to have controllers defined like this:
trait FooCtrl extends Controller { def fooRepo: FooRepo def foo() = Action { fooRepo.findAll() Ok } } object FooCtrl extends FooCtrl { override def fooRepo = FooMongoRepo }where FooMongoRepo is implementation of FooRepo based on play reactivemongo plugin.
Then you can write tests mocking mongo repositories, so you don't have to run DB during tests
class FooCtrlIT extends Specification with Mockito { class FooCtrlTest extends FooCtrl { val fooRepo = mock[FooRepo] //if you wonder //why it is val, hmmm ... mock //verifying doesn't work for me on override def } "Foo controler" should { "return ok" in { val fooTest = new FooCtrlTest val request = FakeRequest("GET", "/api/foos") val result = fooTest.foo()(request) status(result) must equalTo(OK) } } }
Simple?
Some problem might occur if you need run test in
running(FakeApplication())
(Some of your classes eg. session parameters encryption needs running application) You can spot errors like:
[error] r.c.a.MongoDBSystem - (State: Closing) UNHANDLED MESSAGE: ChannelConnected(-951772015 [info] application - ReactiveMongo Connections stopped. [Success(Closed)] [INFO] [07/31/2014 21:12:39.933] [application-akka.actor.default-dispatcher-2] [akka://application/user/$b] Message [reactivemongo.core.actors.Close$] from Actor[akka://application/deadLetters] to Actor[akka://application/user/$b#162091841] was not delivered. [1] dead letters encountered. This logging can be turned off or adjusted with configuration settings 'akka.log-dead-letters' and 'akka.log-dead-letters-during-shutdown'.
The solution is to run test without mongo plugin. You don't need it because you mock all repositories.
running(FakeApplication(withoutPlugins = Seq("play.modules.reactivemongo.ReactiveMongoPlugin"))) { ... }
If it helps, then leave me some comment. Thx.
No comments:
Post a Comment