在我的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
文件并在我的测试用例中使用它?
我的测试用例已经扩展了OneAppPerSuiteWithComponents
。我使用components.messagesApi
的OneAppPerSuiteWithComponents
已经可用并从messages
文件中读取