在一个新项目中,我们使用 MvcMailer,它很棒,我想知道如何使用 Rhino 和 NUnit 测试它?还有一个post on SO和一个good Wiki page但这不是我要找的。对于我的 Controller ,我通常使用 MvcContrib's Testhelper 来测试它们
- 我首先尝试模拟邮件程序类,但如果这样做,我无法验证我的 ViewBag 数据,我的 PopulateBody 方法遇到问题,我必须构建自己的 IMailerBase 接口(interface)
- 我尝试在此之后测试邮件程序,就像使用 MvcContrib 测试 Controller 一样,但它只接受 InitializeController() 中的 Controller 对象,因此它不起作用。
- MailerBase.cs class 中还有一个 IsTestModeEnabled 属性但是当我对其进行测试时,我在空 URI 上收到错误。
不知道最好的方法是什么,我正在寻求帮助,谢谢大家!
最佳答案
这是我的代码:
// arrange
var passwordMailer = MockRepository.GeneratePartialMock<PasswordMailer>();
passwordMailer.Stub(mailer => mailer.PopulateBody(Arg<MailMessage>.Is.Anything, Arg.Is("ForgotPassword"), Arg<string>.Is.Null, Arg<Dictionary<string, string>>.Is.Null));
// act
var mailMessage = passwordMailer.ForgotPassword("<a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="daaebfa9ae9abfa2bbb7aab6bff4b9b5b7" rel="noreferrer noopener nofollow">[email protected]</a>", "4454-8838-73773");
// assert
Assert.AreEqual(string.Format(Login.mailBody, "4454-8838-73773"), passwordMailer.ViewBag.Body);
Assert.AreEqual(Login.mailSubject, mailMessage.Subject);
Assert.AreEqual("<a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="e296879196a2879a838f928e87cc818d8f" rel="noreferrer noopener nofollow">[email protected]</a>", mailMessage.To.First().ToString());
正如您所看到的,您可以使用Rhino 的partialMock 功能来实现它。
关于c# - MvcMailer 使用Rhino Mock 进行单元测试,如何进行?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5643928/