Grails:集成测试邮件插件
我正在尝试集成测试使用邮件插件的类。当我运行测试(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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
查看插件作者自己对邮件插件的测试后,位于 https://github.com/gpc/grails-mail/blob/master/test/integration/org/grails/mail/MailServiceTests.groovy 我意识到值中的路径视图参数全部以“/”开头。我将方法更改为
现在它可以在集成测试和正常程序执行中使用。
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
And now it works in integration tests and normal program execution.
sendMail(..)
方法中的body
参数是一个带有键view
、model
和插件
。plugin
的值是必需的,并指向一些其他支持插件,例如该相应插件的名称“email-confirmation”。您的错误消息将在
org.grails.mail.MailMessageBuilder.renderMailView(Object, Object, Object)
中抛出。您可以在 Grails 项目的 plugin 文件夹中找到此类。不幸的是,我没有找到太多关于邮件插件的文档。因此,目前我还不能轻易说出如何使用上述支持插件。但是,如果您无法继续前进,我可能会尝试进一步调查。谢谢
The
body
parameter in thesendMail(..)
method is a map with the keysview
,model
, andplugin
. A value forplugin
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