Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/unit-testing/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Unit testing MvcMailer单元测试:System.ArgumentNullException httpContext不能为null_Unit Testing_Asp.net Mvc 3_Moq_Mvcmailer - Fatal编程技术网

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

Unit testing MvcMailer单元测试:System.ArgumentNullException httpContext不能为null,unit-testing,asp.net-mvc-3,moq,mvcmailer,Unit Testing,Asp.net Mvc 3,Moq,Mvcmailer,我无法使用visual studio测试套件和Moq成功运行MvcMailer的单元测试。 我已逐字复制了wiki中的示例,但每次都会出现以下异常: Test method MvcMailerTest.Tests.MailerTest.TestMethod1 threw exception: System.ArgumentNullException: Value cannot be null. Parameter name: httpContext 代码如下:(使用VS单元测试框架-与示例中

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

Test method MvcMailerTest.Tests.MailerTest.TestMethod1 threw exception: 
System.ArgumentNullException: Value cannot be null.
Parameter name: httpContext
代码如下:(使用VS单元测试框架-与示例中使用nUnit时的错误完全相同)

维基在这里:

类似(几乎完全相同)的问题:


有人知道如何解决这个问题吗?链接的问题说我需要模仿我已经做过的PopulateBody方法(根据wiki)

您可能也需要模拟HttpContext。您可以通过创建HttpContextBase模拟对象并将其分配给控制器对象来实现此目的。

我怀疑这是因为您正在将新模拟重新分配给
\u userMailerMock
变量,因此实际上没有模拟
PopulateBody
方法

var _userMailerMock = new Mock<UserMailer>();
_userMailerMock.Setup(mailer => mailer.PopulateBody(It.IsAny<MailMessage>(), "Welcome", null));
_userMailerMock.CallBase = true;
var\u userMailerMock=new Mock();
_userMailerMock.Setup(mailer=>mailer.PopulateBody(It.IsAny(),“Welcome”,null));
_userMailerMock.CallBase=true;

取出第二个赋值
\u userMailerMock=new Mock()
Callbase=true之前的行

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

PopulateBody(邮件消息,“欢迎”,空)


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

对Filip的答案进行快速补充,有人可能会觉得有用:我使用的是MvcMailer包的4.0版。我在邮件操作中使用了
Populate(Action-Action)
方法,注意到它使用了
PopulateBody的four参数版本:

// Mvc.Mailer.MailerBase (using ILSpy)
public virtual MvcMailMessage Populate(Action<MvcMailMessage> action)
{
    MvcMailMessage mvcMailMessage = new MvcMailMessage();
    action(mvcMailMessage);

    // Four parameters! (comment added by me)
    this.PopulateBody(mvcMailMessage, mvcMailMessage.ViewName, mvcMailMessage.MasterName, mvcMailMessage.LinkedResources);

    return mvcMailMessage;
}

…成功了。

我现在只是想测试邮件程序,而不是测试使用邮件程序的控制器(如果这有什么区别的话!)。哎呀,我错误地将代码粘贴到stackoverflow中了。代码中只有一个对_userMailerMock的分配。仍然会收到相同的错误。这已发送给MVC Mailer团队,但未经解决而关闭。有人有解决方法吗?@Josh,不幸的是我还没有找到解决方法。我猜这个bug在MVC mailer开发人员手中……一个解决方法是将代码更改为:
PopulateBody(mailMessage,“Welcome”,null)这将起作用,因为您有一个模拟设置用于PopulateBody重载,而不是它的2参数版本。@FilipCornelissen:终于有了一个有效的解决方案!谢谢你,伙计……:)@菲利普科内利森:你不想把你的评论作为一个答案,这样我就可以接受了吗?谢谢!Wiki应更新为新版本。干得好
var _userMailerMock = new Mock<UserMailer>();
_userMailerMock.Setup(mailer => mailer.PopulateBody(It.IsAny<MailMessage>(), "Welcome", null));
_userMailerMock.CallBase = true;
// Mvc.Mailer.MailerBase (using ILSpy)
public virtual MvcMailMessage Populate(Action<MvcMailMessage> action)
{
    MvcMailMessage mvcMailMessage = new MvcMailMessage();
    action(mvcMailMessage);

    // Four parameters! (comment added by me)
    this.PopulateBody(mvcMailMessage, mvcMailMessage.ViewName, mvcMailMessage.MasterName, mvcMailMessage.LinkedResources);

    return mvcMailMessage;
}
PopulateBody(mailMessage, "Welcome", "SomeMasterName", null);