[scaldi-play 0.5.8] i18n in test

118 views
Skip to first unread message

Ulrik Lejon

unread,
Jun 30, 2015, 2:12:37 PM6/30/15
to sca...@googlegroups.com
Hi

Lets say I have a service which should be able to create different strings depending on selected language. For example an email service:

import play.api.i18n.Messages

class EmailService {

    def createEmail()(implicit messages: Messages): String = {
        s"""Subject: ${messages("email.subject")}. Message: ${messages("email.message")}"""
    }
}


If I want to unit test this class I would do the following:

import org.specs2.mutable.Specification
import play.api.Logger
import play.api.i18n.Messages
import service.EmailService
import org.specs2.mock._

class EmailServiceTest extends Specification with Mockito {

    "EmailService" should {

        "be able to create an email with a mocked Messages" in {
            implicit val messagesMock = mock[Messages]
            messagesMock.apply(===("email.subject"), anyVarArg) returns "Mocked subject"
            messagesMock.apply(===("email.message"), anyVarArg) returns "Mocked message"


            val email = new EmailService().createEmail()
            email mustEqual s"""Subject: Mocked subject. Message: Mocked message"""
        }

    }
}

But what If I want to test it with a running application, like this:

import org.specs2.mutable.Specification
import play.api.Logger
import play.api.i18n.Messages
import scaldi.play.ScaldiApplicationBuilder._
import service.EmailService

class EmailServiceTest extends Specification {

    "EmailService" should {

        "be able to create an email with an application running" in {
            withScaldiApp() {
                val email = new EmailService().createEmail()
                Logger.error(email)
                email mustEqual s"""Subject: Welcome. Message: Welcome to the future"""
            }
        }


    }
}

My messages.en file:

email.subject=Welcome
email.message=Welcome to the future

If I try to run this test I get:

[play-i18n] $ test
[info] Compiling 1 Scala source to /source/tmp/play-i18n/target/scala-2.11/test-classes...
[error] /source/tmp/play-i18n/test/EmailServiceTest.scala:24: could not find implicit value for parameter messages: play.api.i18n.Messages
[error]                 val email = new EmailService().createEmail()
[error]                                                           ^
[error] one error found
[error] (test:compileIncremental) Compilation failed
[error] Total time: 2 s, completed Jun 30, 2015 8:04:03 PM


So, how do I get hold of the "real" Messages/MessagesApi class in this case?

cheers,
Ulrik

Oleg Ilyenko

unread,
Jun 30, 2015, 3:08:23 PM6/30/15
to sca...@googlegroups.com, ulrik...@gmail.com
Hi Ulrik,

In your scenario you need to construct `Messages` somehow and ensure that this `Messages` uses `MessagesApi` that comes directly from the application. It's pretty easy to do - you just need to inject `MessagesApi` and use it to construct implicit `Messages` object (don't forget that you also need to specify the language in order to do this):

import scaldi.Injectable._

"be able to create an email with an application running" in {
  withScaldiInj() { implicit Inj =>
    implicit val messages = new Messages(Lang("en"), inject [MessagesApi])

    val email = new EmailService().createEmail()
    email mustEqual s"""Subject: Welcome. Message: Welcome to the future"""
  }
}

I also used `withScaldiInj` here - it is very similar to `withScaldiApp`, but also gives you access to the `Injector`.

BTW, since `EmailService` already defined in application, you can just inject it instead of creating a new one. It can be very helpful, if `EmailService` have dependencies on it's own:

import scaldi.Injectable._

"be able to create an email with an application running" in {
  withScaldiInj() { implicit Inj =>
    val emailService = inject [EmailService]
    implicit val messages = new Messages(Lang("en"), inject [MessagesApi])

    val email = emailService.createEmail()
    email mustEqual s"""Subject: Welcome. Message: Welcome to the future"""
  }
}

Cheers,
Oleg

Ulrik Lejon

unread,
Jul 10, 2015, 5:13:35 AM7/10/15
to sca...@googlegroups.com, ulrik...@gmail.com
Awsome, works great!

Cheers,
Ulrik
Reply all
Reply to author
Forward
0 new messages