MvcMailer:无法在使用 Url.Action 的 Razor 视图上完成 NUnit 测试

发布于 2024-10-30 12:21:52 字数 946 浏览 0 评论 0原文

这是我的问题 - 我正在使用 MvcMailer 使用 Razor 语法创建格式良好的电子邮件,它是一个很棒的工具用于此目的!

我遇到的问题是这样的 - 这是我发送的一封电子邮件的视图中的一些语法:

<p>Click here to return to <a href="@Url.Abs(Url.Action("Details", "Home", new{ Id=ViewBag.IdeaId}))">@ViewBag.IdeaName</a></p>

每当我尝试运行单元测试时,我都会收到以下错误消息:

我们可以发送新评论的电子邮件通知吗?:System.ArgumentNullException:值不能为空。 参数名称:httpContext

Stacktrace - 为了简洁而缩短,仅相关部分:

在 System.Web.Routing.RouteCollection.GetRouteData(HttpContextBase httpContext) 在 Mvc.Mailer.MailerBase.CreateControllerContext() 在Mvc.Mailer.MailerBase.ViewExists(字符串viewName,字符串masterName) 在 Castle.Proxies.Invoices.MailerBase_ViewExists.InvokeMethodOnTarget() 在 Castle.DynamicProxy.AbstractInspiration.Proceed()

MvcMailer 方法,而不必模拟控制器上下文中的所有内容一直到路由结果?

Here's my problem - I am using MvcMailer to create nicely formatted emails using Razor syntax, and it's a great tool to use for that!

The issue I'm running into is this - here's some syntax from my View for one of the emails i send:

<p>Click here to return to <a href="@Url.Abs(Url.Action("Details", "Home", new{ Id=ViewBag.IdeaId}))">@ViewBag.IdeaName</a></p>

Whenever I try to run my unit tests, I get the following error message:

Can we send out email notifications for new comments?: System.ArgumentNullException : Value cannot be null.
Parameter name: httpContext

Stacktrace - shortened for brevity, relevant sections only:

at System.Web.Routing.RouteCollection.GetRouteData(HttpContextBase httpContext)
at Mvc.Mailer.MailerBase.CreateControllerContext()
at Mvc.Mailer.MailerBase.ViewExists(String viewName, String masterName)
at Castle.Proxies.Invocations.MailerBase_ViewExists.InvokeMethodOnTarget()
at Castle.DynamicProxy.AbstractInvocation.Proceed()

The issue is that my HttpContext is null - is there a straightforward way to unit test this MvcMailer method without having to mock everything from the controller context all the way down the route results?

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

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

发布评论

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

评论(2

哽咽笑 2024-11-06 12:21:52

您可以查看MvcMailer wiki 您需要做的就是模拟 PopulateBody 方法,然后它将绕过视图渲染作为测试的一部分。它应该类似于以下内容:

_userMailerMock.Setup(mailer => mailer.PopulateBody(It.IsAny<MailMessage>(), "Welcome", null));

希望这有帮助!

You can take a look at the section titled Unit Test Your Mailers at the MvcMailer wiki All you need to do is, just mock out the PopulateBody method and then it will bypass the view rendering as a part of the testing. It should look something like the following:

_userMailerMock.Setup(mailer => mailer.PopulateBody(It.IsAny<MailMessage>(), "Welcome", null));

Hope this helps!

天赋异禀 2024-11-06 12:21:52

这种语法对我有用:

var userMailerMock = new Mock<UserMailer> {CallBase = true};
userMailerMock.Setup(mailer => mailer.PopulateBody(It.IsAny<MailMessage>(), It.IsAny<string>(), It.IsAny<string>(), It.IsAny<Dictionary<string, string>>()));

您可能还想模拟其他重载(如果上述内容没有帮助或只是为了确定):

userMailerMock.Setup(mailer => mailer.PopulateBody(It.IsAny<MailMessage>(), It.IsAny<string>(), It.IsAny<Dictionary<string,string>>()));

This syntax worked for me:

var userMailerMock = new Mock<UserMailer> {CallBase = true};
userMailerMock.Setup(mailer => mailer.PopulateBody(It.IsAny<MailMessage>(), It.IsAny<string>(), It.IsAny<string>(), It.IsAny<Dictionary<string, string>>()));

You might want to mock the other overload as well (if the above doesn't help or just to be sure):

userMailerMock.Setup(mailer => mailer.PopulateBody(It.IsAny<MailMessage>(), It.IsAny<string>(), It.IsAny<Dictionary<string,string>>()));
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文