Hi All,
One quirk I found whilst upgrading play-recaptcha to Play 2.4 was updating my unit tests to use the new i18n APIs. I'm using specs2, and have unit tests for my templates and some utility code that take an implicit Messages instance as input.
The best solution I found was the following code to explicitly create a Messages instance for a given request and application instance. However this seems brittle and will surely break if the i18n implementation is refactored in the future.
private def getMessages(request: Request[AnyContent]): (Messages) = {
val app = Play.current
val env = new Environment(app.path, app.classloader, app.mode)
val messagesApi = new DefaultMessagesApi(env, app.configuration,
new DefaultLangs(app.configuration))
messagesApi.preferred(request)
}
Is there not a better way of doing this? When testing with other DI injected objects (like WSClient) I use a mock to check my code behaviour. But with Messages I want a real one so I can test that i18n is working properly. The Play 2.4 documentation doesn't seem to cover using DI to inject real instances of objects under test, because I want to test real behaviour not inject a mock object?
Is this functionality that could be added to play.api.test perhaps?
Cheers,
Chris