MvcMailer单元测试:System.ArgumentNullException httpContext不能为null

wog*_*les 8 unit-testing moq mvcmailer asp.net-mvc-3

我无法使用visual studio测试套件和Moq成功运行MvcMailer的单元测试.我已经逐字逐句地复制了wiki中的示例,但每次都得到以下异常:

Test method MvcMailerTest.Tests.MailerTest.TestMethod1 threw exception: 
System.ArgumentNullException: Value cannot be null.
Parameter name: httpContext
Run Code Online (Sandbox Code Playgroud)

代码如下:(使用VS单元测试框架 - 在使用nUnit时完全相同的错误,如示例中所示)

        //Arrange: Moq out the PopulateBody method
        var _userMailerMock = new Mock<UserMailer>();
        _userMailerMock.Setup(mailer => mailer.PopulateBody(It.IsAny<MailMessage>(), "Welcome", null));            
        _userMailerMock.CallBase = true;

        //Act
        var mailMessage = _userMailerMock.Object.Welcome();
Run Code Online (Sandbox Code Playgroud)

在Welcome()方法中的以下行失败(直接从wiki复制):

 PopulateBody(mailMessage, viewName: "Welcome");
Run Code Online (Sandbox Code Playgroud)

维基在这里:https://github.com/smsohan/MvcMailer/wiki/MvcMailer-Step-by-Step-Guide

类似(几乎完全相同)问题:MvcMailer:无法在使用Url.Action的Razor Views上完成NUnit测试

任何人都知道如何解决/解决这个问题?链接的问题说我需要模拟我已经完成的PopulateBody方法(根据维基).

Fil*_*sen 2

解决方法是将您的代码更改为:

PopulateBody(mailMessage, "Welcome", null);

这会起作用,因为您有一个针对 PopulateBody 重载的模拟设置,而不是针对它的 2 参数版本。