Pet*_*ter 13 testing service grails internationalization grails-2.0
我在Grails 2.0项目的一个服务中使用消息包来获取国际化文本.用例是一个通过邮件插件以异步方式发送的电子邮件主题,因此在控制器或TagLib中使用它是没有意义的(考虑到通常的参数是不访问服务中的文本或视图) ).这段代码在我运行的Grails应用程序中运行良好,但我不确定如何测试它.
我PluginAwareResourceBundleMessageSource在我的defineBeans中尝试过,因为这是我正在运行的应用程序注入的内容,但它导致了nullpointers,因为它似乎需要围绕插件管理器进行一系列设置,这样我的测试环境就不会给予(甚至集成).
然后我尝试了一个ReloadableResourceBundleMessageSource纯粹的Spring,但它似乎无法看到我的.properties文件,并且在代码'my.email.subject'下找到了没有消息的locale'en'失败.
我觉得我有点陷入虫洞,因为在服务中访问Grails i18n没有记录在grails文档中,所以如果有一个首选的方法,请告诉我.
请注意我的.properties文件位于标准grails-app/i18n位置.
考试
@TestFor(EmailHelperService)
class EmailHelperServiceTests {
void testSubjectsDefaultLocale() {
defineBeans {
//messageSource(PluginAwareResourceBundleMessageSource); Leads to nullpointers
messageSource(ReloadableResourceBundleMessageSource);
}
String expected = "My Expected subject Passed1 Passed2";
String actual = service.getEmailSubjectForStandardMustGiveGiftFromBusiness(Locale.ENGLISH, Passed1 Passed2);
assertEquals("email subject", expected, actual);
}
Run Code Online (Sandbox Code Playgroud)
服务:
class EmailHelperService {
def messageSource;
public String getEmailSubject(Locale locale, String param1, String param2) {
Object[] params = [param1, param2].toArray();
return messageSource.getMessage("my.email.subject", params, locale );
}
Run Code Online (Sandbox Code Playgroud)
Gra*_*her 29
在Grails的单元测试中已经有一个messageSource,它是一个StaticMessageSource(参见http://static.springsource.org/spring/docs/2.5.4/api/org/springframework/context/support/StaticMessageSource.html),您可以使用addMessage方法添加模拟消息:
messageSource.addMessage("foo.bar", request.locale, "My Message")
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
9021 次 |
| 最近记录: |