MvcMailer:无法完成对剃刀查看NUnit的测试,其中使用Url.Action [英] MvcMailer: Can't complete NUnit tests on Razor Views which use Url.Action

查看:155
本文介绍了MvcMailer:无法完成对剃刀查看NUnit的测试,其中使用Url.Action的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

下面是我的问题 - 我使用 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:

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

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:

在System.Web.Routing.RouteCollection.GetRouteData(HttpContextBase的HttpContext)
  在Mvc.Mailer.MailerBase.CreateControllerContext()
  在Mvc.Mailer.MailerBase.ViewExists(字符串的viewName,字符串masterName)
  在Castle.Proxies.Invocations.MailerBase_ViewExists.InvokeMethodOnTarget()
  在Castle.DynamicProxy.AbstractInvocation.Proceed()

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()

问题是,我的HttpContext为空 - 有没有单元测试这种方法MvcMailer一个简单的方法,而不必从控制器上下文一路嘲笑都记录下来的路线的结果。

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?

推荐答案

您可以看看在本节标题为单元的的 =htt​​ps://开头github上。 COM / smsohan / MvcMailer /维基/ MvcMailer步由步指南相对=nofollow> MvcMailer维基所有你需要做的,只是模拟出了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:

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

希望这有助于!

这篇关于MvcMailer:无法完成对剃刀查看NUnit的测试,其中使用Url.Action的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆