【问题标题】:Grails: Integration testing the Mail PluginGrails:邮件插件的集成测试
【发布时间】:2011-05-05 04:30:08
【问题描述】:

我正在尝试集成测试一个使用邮件插件的类。当我运行我的测试(grails test-app -integration EmailerIntegration)时,我得到了错误:

找不到邮件正文布局/_email。它在插件中吗?如果是这样,您必须在 [plugin] 变量中传递插件名称

我的测试用例的 setUp 方法中是否缺少一些初始化代码?

这是测试用例的代码:

package company

import grails.test.*

class EmailerIntegrationTests extends GrailsUnitTestCase {
    protected void setUp() {
        super.setUp()
    }

    protected void tearDown() {
        super.tearDown()
    }

    void testSomething() {
        User owner = new User()
        owner.displayName = "Bob"
        owner.email = "bob@yahoo.com"

        Emailer emailer = new Emailer()
        emailer.sendReadyEmail(owner)
    }
}

这是被测试类的代码:

package company

import org.apache.log4j.Logger;
import org.codehaus.groovy.grails.commons.ApplicationHolder;
import org.springframework.context.ApplicationContext;

class Emailer {
    private Logger log = Logger.getLogger(this.getClass());
    ApplicationContext ctx = (ApplicationContext)ApplicationHolder.getApplication().getMainContext();
    def mailService = ctx.getBean("mailService");

    def sendReadyEmail = { owner ->
            mailService.sendMail {
                    to owner.email
                    subject "Ready to go"
                    body( view:"layouts/_email", model:[ownerInstance:owner])
            }
    }
}

谢谢,

埃弗雷特

【问题讨论】:

    标签: email grails grails-plugin


    【解决方案1】:

    在查看了插件作者自己在https://github.com/gpc/grails-mail/blob/master/test/integration/org/grails/mail/MailServiceTests.groovy 对邮件插件的测试后,我意识到视图参数值中的路径都以“/”开头。我将方法更改为

    def sendReadyEmail = { owner ->
            mailService.sendMail {
                    to owner.email
                    subject "Ready to go"
                    body( view:"/layouts/_email", model:[ownerInstance:owner])
            }
    

    现在它可以在集成测试和正常程序执行中使用。

    【讨论】:

      【解决方案2】:

      sendMail(..) 方法中的body 参数是一个带有view、model 和plugin 键的映射。 plugin 的值是必需的,并指向 一些 其他支持插件,例如相应插件的名称“电子邮件确认”。

      您的错误消息在org.grails.mail.MailMessageBuilder.renderMailView(Object, Object, Object) 中抛出。您可以在 Grails 项目的 plugin 文件夹中找到此类。

      不幸的是,我没有找到太多关于 Mail 插件的文档。因此,目前,我无法轻易说出如何使用上述支持插件。但是,如果您无法前进,我可能会尝试进一步调查。谢谢

      【讨论】:

      • 不走运。我无法为使测试工作的插件密钥找到一个值。
      • 如前所述,一个可能的键是“email-confirmation”。 - 在此期间,您是否找到任何相关文件,或者您是否有任何其他发现? - 没有像“请为我解决这个问题”这样片面的事情。 - 如果你努力的话,我有兴趣进一步调查,但我不做别人的功课。 - 我们都可能会得出这样的结论:文档太少,导致我们无法使用这些插件。 - 谢谢
      • 我应该更具体一些,我尝试了一些插件键的值,例如“email-confirmation”、“email”和“spring-events”(我碰巧使用的另一个插件)。每次尝试都会遇到异常。然而,在找到一些额外的时间来深入研究这个问题后,我找到了答案。
      • 感谢您发布反馈和解决方案,很抱歉我帮不上忙。 - 恭喜! - 大多数 Grails 插件的非文档化真的很荒谬。那是“通过反复试验进行编程”和“通过逆向工程”......
      猜你喜欢
      • 2011-10-22
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-04-06
      • 1970-01-01
      • 2012-02-11
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多