如何在 Grails 2.0 服务中对 i18n 注入 messageSource 的使用进行单元或集成测试 [英] How to unit or integration test use of injected messageSource for i18n in Grails 2.0 service

查看:19
本文介绍了如何在 Grails 2.0 服务中对 i18n 注入 messageSource 的使用进行单元或集成测试的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在 Grails 2.0 项目中的一项服务中使用消息包来处理国际化文本.用例是通过邮件插件以异步方式发送的电子邮件主题,因此将其放在控制器或 TagLib 中确实没有意义(考虑到通常的论点是不访问服务中的文本或视图).这段代码在我正在运行的 Grails 应用程序中运行良好,但我不确定如何测试它.

I make use of a message bundle in one of my services in a Grails 2.0 project for internationalized text. The use case is an email subject that is sent via the mail plugin in an asynchronous way, so it really doesn't make sense to have this in a controller or TagLib (given the usual argument of not accessing your text or views in a service). This code works fine in my running Grails app, but I'm not sure how to test it.

我在我的 defineBeans 中尝试了一个 PluginAwareResourceBundleMessageSource,因为这是我正在运行的应用程序注入的内容,但它导致了空指针,因为它看起来需要围绕插件管理器进行大量设置,因此我的测试环境是不给予(甚至整合).

I tried a PluginAwareResourceBundleMessageSource in my defineBeans as that is what my running application injects, but it led to nullpointers as it appears it needs a bunch of setup around plugin managers and such that my test environment is not giving (even integration).

然后我尝试了 ReloadableResourceBundleMessageSource,因为它是纯 Spring,但它似乎看不到我的 .properties 文件,并且在代码my.email.subject"下找不到消息而失败对于语言环境en".

I then tried a ReloadableResourceBundleMessageSource as it was pure Spring, but it can't seem to see my .properties files, and fails with a No message found under code 'my.email.subject' for locale 'en'.

我觉得我正在进入一个虫洞,因为 grails 文档中没有记录在服务中访问 Grails i18n,所以如果有更好的方法可以做到这一点,请告诉我.

I feel like I'm going down a wormhole a bit as accessing Grails i18n in a service is not documented in the grails docs, so if there is a preferred way to do this, let me know.

请注意,我的 .properties 文件位于标准 grails-app/i18n 位置.

Note my .properties file is in the standard grails-app/i18n location.

测试

@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);

}

服务:

    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 );      
    }

推荐答案

Grails的单元测试中已经有一个messageSource,它是一个StaticMessageSource(见http://static.springsource.org/spring/docs/2.5.4/api/org/springframework/context/support/StaticMessageSource.html),您可以使用 addMessage 方法添加模拟消息:

There is already a messageSource in unit tests in Grails, it is a StaticMessageSource (see http://static.springsource.org/spring/docs/2.5.4/api/org/springframework/context/support/StaticMessageSource.html), you can add mock messages with the addMessage method:

messageSource.addMessage("foo.bar", request.locale, "My Message")

这篇关于如何在 Grails 2.0 服务中对 i18n 注入 messageSource 的使用进行单元或集成测试的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆