如何使用消息文件创建自己的messagesApi

问题描述 投票:0回答:1

在我的play-framework应用程序中,有一个消息文件,我可以用它来定义我自己的消息。例如

##############Application Error messages

error.incorrectBodyType=Incorrect body type. Body type must be JSON
error.incorrectBodyContentInLoginRequest=No Login info found in the request. Check format
error.incorrectUsernameOrPassword = Incorrect username or password
error.unregisteredUser = registration not complete
error.ghostUsername = No record found for the username
error.unauthenticated = You must first sign in
error.unauthorized = You are not permitted

####### Application Success Messages
success.signoutSuccessful = Signout Successful

游戏框架使我的Controller可以使用这些消息,我可以像messagesApi("error.unauthenticated")(langs.availables(0))一样使用它们。

我无法弄清楚如何在单元测试中使用messages文件。我正在使用编译时注入,并创建自己的所需类的实例。要创建MessagesApi,有一个DefaultMessagesApi方法,但它需要一个map,而不是File

我可以在单元测试中创建消息,如下所示,但我必须复制从messages文件复制消息到此map的工作量

val messagesApi = new DefaultMessagesApi( //takes map of maps. the first is the language file, the 2nd is the map between message title and description
    Map("en" -> //the language file
      Map("error.incorrectBodyType" -> "Incorrect body type. Body type must be JSON",
        "error.incorrectUsernameOrPassword"->"Incorrect username or password",
        "error.incorrectBodyContentInLoginRequest"->"No Login info found in the request. Check format", //map between message title and description
        "error.unregisteredUser" -> "registration not complete",
        "error.ghostUsername" -> "No record found for the username",
        "success.signoutSuccessful" -> "Signout Successful",
        "error.unauthenticated" -> "You must first sign in",
        "error.unauthorized" -> "You are not permitted")

    )
  )

有没有办法我可以读取messages文件并在我的测试用例中使用它?

playframework-2.6
1个回答
0
投票

我的测试用例已经扩展了OneAppPerSuiteWithComponents。我使用components.messagesApiOneAppPerSuiteWithComponents已经可用并从messages文件中读取

© www.soinside.com 2019 - 2024. All rights reserved.