MvcMailer单元测试信息:System.ArgumentNullException的HttpContext不能为空 [英] MvcMailer unit tests: System.ArgumentNullException httpContext cannot be null
问题描述
我无法成功运行使用Visual Studio的测试套件和起订量为MvcMailer单元测试。
我从字维基字复制的例子,但每次都获得以下异常:
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如下:(使用VS单元测试框架 - 使用NUnit作为例子,当完全相同的错误)
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");
维客是在这里:<一href=\"https://github.com/smsohan/MvcMailer/wiki/MvcMailer-Step-by-Step-Guide\">https://github.com/smsohan/MvcMailer/wiki/MvcMailer-Step-by-Step-Guide
类似的(几乎完全一样)问题:<一href=\"http://stackoverflow.com/questions/5575964/mvcmailer-cant-complete-nunit-tests-on-razor-views-which-use-url-action\">MvcMailer:无法完成上使用Url.Action
Similar (almost exactly the same) Question: MvcMailer: Can't complete NUnit tests on Razor Views which use Url.Action
任何人都知道如何解决/解决这个问题?链接的问题,说我需要模拟出这是我做了(根据维基)的PopulateBody方法。
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).
推荐答案
一个解决方法是改变你的code这样:
a workaround is to change your code to this:
PopulateBody(MAILMESSAGE,欢迎光临,NULL);
这会工作,因为你有PopulateBody那过载和一个模拟设置不为它的2参数版本。
This will work because you have a mock setup for that overload of PopulateBody and not for the 2 parameter version of it..
这篇关于MvcMailer单元测试信息:System.ArgumentNullException的HttpContext不能为空的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!