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

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

问题描述

我在Grails 2.0项目的一个服务中为国际化文本使用了一个消息包。用例是一个通过邮件插件以异步方式发送的电子邮件主题,所以将它放在控制器或TagLib中是没有意义的(考虑到通常不会访问服务中的文本或视图)。此代码在我运行的Grails应用程序中正常工作,但我不知道如何测试它。

我在我的defineBeans中尝试了一个 PluginAwareResourceBundleMessageSource ,因为这是我运行的应用程序注入的内容,但它导致nullpointers为看起来它需要围绕插件管理器进行一系列设置,这样我的测试环境就不会提供(甚至是集成)。

然后我尝试了一个 ReloadableResourceBundleMessageSource ,因为它是纯粹的Spring,但似乎看不到我的.properties文件,并且在代码为'my.email.subject'的地方'en'找到没有消息。



我觉得我要去一个虫洞a因为在服务中访问Grails i18n没有记录在grails文档中,所以如果有一个首选的方法来做到这一点,让我知道。



请注意我的.properties文件位于标准位置。




  @TestFor(EmailHelperService)
class EmailHelperServiceTests {

void testSubjectsDefaultLocale(){
defineBeans {
// messageSource(PluginAwareResourceBundleMessageSource);导致nullpointers
messageSource(ReloadableResourceBundleMessageSource);

}
预期字符串=我的预期科目合格1合格2;
String actual = service.getEmailSubjectForStandardMustGiveGiftFromBusiness(Locale.ENGLISH,Passed1 Passed2);
assertEquals(email subject,预期,实际);

$ b

服务:

  class EmailHelperService {
def messageSource;

public String getEmailSubject(Locale locale,String param1,String param2){
Object [] params = [param1,param2] .toArray();
返回messageSource.getMessage(my.email.subject,params,locale);
}


解决方案

Grails中的单元测试,它是一个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)


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.

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).

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'.

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.

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

The test

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

}

Service:

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

解决方案

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天全站免登陆