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

发布于 2024-11-08 19:50:04 字数 1297 浏览 0 评论 0原文

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

Test method MvcMailerTest.Tests.MailerTest.TestMethod1 threw exception: 
System.ArgumentNullException: Value cannot be null.
Parameter name: httpContext

代码如下:(使用 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();

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

 PopulateBody(mailMessage, viewName: "Welcome");

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

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

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

I cannot successfully run unit tests for MvcMailer using the visual studio test suite and Moq.
I have copied the example from the wiki word for word but get the following exception every time:

Test method MvcMailerTest.Tests.MailerTest.TestMethod1 threw exception: 
System.ArgumentNullException: Value cannot be null.
Parameter name: httpContext

Code is as follows: (Using the VS unit test framework - exact same error when using nUnit as in the example)

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

Fails on the following line in the Welcome() method (copied straight out of the wiki):

 PopulateBody(mailMessage, viewName: "Welcome");

The wiki is here: https://github.com/smsohan/MvcMailer/wiki/MvcMailer-Step-by-Step-Guide

Similar (almost exactly the same) Question: MvcMailer: Can't complete NUnit tests on Razor Views which use Url.Action

Anyone know how to fix/get around this? The linked question says I need to mock out the PopulateBody method which I have done (as per wiki).

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

素手挽清风 2024-11-15 19:50:04

对 Filip 的答案的快速补充可能会对某人有所帮助:我正在使用 MvcMailer 包的 4.0 版。我在邮件程序操作中使用 Populate(Actionaction) 方法,并注意到它使用 四个 参数版本 < code>PopulateBody:

// 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);

成功了。

A quick addition to Filip's answer that somebody might find helpful: I am using version 4.0 of the MvcMailer package. I was using the Populate(Action<MvcMailMessage> action) method inside my mailer actions and noticed that it uses a four-parameter version of PopulateBody:

// 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;
}

As such, I found that setting up the mock with four parameters...

PopulateBody(mailMessage, "Welcome", "SomeMasterName", null);

...did the trick.

婴鹅 2024-11-15 19:50:04

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

PopulateBody(mailMessage, "Welcome", null);

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

a workaround is to change your code to this:

PopulateBody(mailMessage, "Welcome", null);

This will work because you have a mock setup for that overload of PopulateBody and not for the 2 parameter version of it..

只有影子陪我不离不弃 2024-11-15 19:50:04

您可能还需要模拟 HttpContext。您可以通过创建 HttpContextBase 模拟对象并将其分配给您的 Controller 对象来完成此操作。

You probably need to mock HttpContext too. You can do this by creating a HttpContextBase mock object and assign it to your Controller object.

谢绝鈎搭 2024-11-15 19:50:04

我怀疑这是因为您正在将新的模拟重新分配给 _userMailerMock 变量,因此实际上并未模拟 PopulateBody 方法。

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

取出 Callbase = true; 之前的第二个赋值 _userMailerMock = new Mock();

I suspect it is due to that you are reassigning a new mock to the _userMailerMock variable and therefore not actually mocking the PopulateBody method.

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

Take out the second assignment _userMailerMock = new Mock<UserMailer>(); the line before Callbase = true;

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文