Grails:集成测试邮件插件

发布于 2024-09-30 19:29:06 字数 1528 浏览 1 评论 0原文

我正在尝试集成测试使用邮件插件的类。当我运行测试(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 = "[email protected]"

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

谢谢,

Everett

I'm trying to integration test a class that uses the Mail Plugin. When I run my test (grails test-app -integration EmailerIntegration) I get the error:

Could not locate mail body layouts/_email. Is it in a plugin? If so you must pass the plugin name in the [plugin] variable

Is there some initialization code I'm missing from the setUp method of my test case?

Here is the code for the test case:

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 = "[email protected]"

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

Here is the code for the class being tested:

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

Thanks,

Everett

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

可是我不能没有你 2024-10-07 19:29:06

查看插件作者自己对邮件插件的测试后,位于 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])
        }

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

After looking at the plugin author's own tests for the mail plugin at https://github.com/gpc/grails-mail/blob/master/test/integration/org/grails/mail/MailServiceTests.groovy I realized that the paths in the values for the view parameter all begin with a '/'. I changed my method to

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

And now it works in integration tests and normal program execution.

狼性发作 2024-10-07 19:29:06

sendMail(..) 方法中的 body 参数是一个带有键 viewmodel插件plugin 的值是必需的,并指向一些其他支持插件,例如该相应插件的名称“email-confirmation”。

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

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

The body parameter in the sendMail(..) method is a map with the keys view, model, and plugin. A value for plugin is required, and points to some other, supporting, plugin, for instance, the name "email-confirmation" for that corresponding plugin.

Your error message is thrown in org.grails.mail.MailMessageBuilder.renderMailView(Object, Object, Object). You can find this class in your Grails project's plugin folder.

Unfortunately, I haven't found too much documentation on the Mail plugin. Thus, at the moment, I cannot easily tell about how to use the aforementioned supporting plugins. If you can't get forward, however, I might try to further investigate. Thanks

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文