我無法使用visual studio測試套件和Moq成功運行MvcMailer的單元測試。 我從一個字維基字複製的例子,但每次出現以下情況例外:MvcMailer單元測試:System.ArgumentNullException httpContext不能爲空
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();
上在歡迎()方法將下面的行失敗(複製直出維基):
PopulateBody(mailMessage, viewName: "Welcome");
維基是在這裏:https://github.com/smsohan/MvcMailer/wiki/MvcMailer-Step-by-Step-Guide
類似的(幾乎完全一樣)問:MvcMailer: Can't complete NUnit tests on Razor Views which use Url.Action
任何人都知道如何解決/解決這個問題?鏈接的問題說,我需要嘲笑我所做的PopulateBody方法(按照wiki)。
已向MVC Mailer團隊報告此問題(https://github.com/smsohan/MvcMailer/issues/20),但未經解決就關閉了。有沒有人有解決方法/修復? – jhappoldt 2011-08-25 18:26:47
@Josh,不幸的是,我仍然沒有找到解決方法或解決方法。猜猜這個bug是在MVC郵件發送者手中的... – woggles 2011-08-25 19:10:52
解決方法是將您的代碼更改爲:'PopulateBody(mailMessage,「Welcome」,null);'這會工作,因爲您有一個模擬設置PopulateBody的超載,而不是它的2參數版本。 – 2012-02-28 09:50:30