MvcMailer 单元测试:System.ArgumentNullException httpContext 不能为 null
我无法使用 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
对 Filip 的答案的快速补充可能会对某人有所帮助:我正在使用 MvcMailer 包的 4.0 版。我在邮件程序操作中使用
Populate(Actionaction)
方法,并注意到它使用 四个 参数版本 < code>PopulateBody:因此,我发现用四个参数设置模拟......
成功了。
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 ofPopulateBody
:As such, I found that setting up the mock with four parameters...
...did the trick.
解决方法是将代码更改为:
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..
您可能还需要模拟 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.
我怀疑这是因为您正在将新的模拟重新分配给
_userMailerMock
变量,因此实际上并未模拟PopulateBody
方法。取出
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 thePopulateBody
method.Take out the second assignment
_userMailerMock = new Mock<UserMailer>();
the line beforeCallbase = true;