这是我的问题 - 我正在使用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:Value不能为null。 参数名称:httpContext
Stacktrace - 为简洁而缩短,仅限相关部分:
在System.Web.Routing.RouteCollection.GetRouteData(HttpContextBase httpContext) 在Mvc.Mailer.MailerBase.CreateControllerContext() 在Mvc.Mailer.MailerBase.ViewExists(String viewName,String masterName) 在Castle.Proxies.Invocations.MailerBase_ViewExists.InvokeMethodOnTarget() 在Castle.DynamicProxy.AbstractInvocation.Proceed()
问题是我的HttpContext为null - 是否有一种简单的方法可以对这个MvcMailer方法进行单元测试,而不必在路由结果中一直模拟控制器上下文中的所有内容?
答案 0 :(得分:4)
您可以在MvcMailer wiki查看标题为单元测试您的邮件程序的部分。您只需要模拟PopulateBody方法然后它将绕过视图渲染作为测试的一部分。它看起来应该如下所示:
_userMailerMock.Setup(mailer => mailer.PopulateBody(It.IsAny<MailMessage>(), "Welcome", null));
希望这有帮助!
答案 1 :(得分:1)
这种语法对我有用:
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>>()));