MvcMailer:无法在使用 Url.Action 的 Razor 视图上完成 NUnit 测试
这是我的问题 - 我正在使用 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您可以查看MvcMailer wiki 您需要做的就是模拟 PopulateBody 方法,然后它将绕过视图渲染作为测试的一部分。它应该类似于以下内容:
希望这有帮助!
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:
Hope this helps!
这种语法对我有用:
您可能还想模拟其他重载(如果上述内容没有帮助或只是为了确定):
This syntax worked for me:
You might want to mock the other overload as well (if the above doesn't help or just to be sure):