System.ArgumentNullException
时,System.Web.HttpBrowserCapabilities
构造函数抛出 mail.Send()
。它需要httpBrowserCapabilities
参数。
我使用的代码:
EmailModel:
public class EmailModel :Email
{
public EmailModel() :base("EmailModel")
{
}
public string To = "**********@gmail.com";
[Display(ResourceType = typeof(Resource), Name = "Name")]
[Required(ErrorMessageResourceName = "Error_NameRequired", ErrorMessageResourceType = typeof(Resource))]
public string Name { get; set; }
[DataType(DataType.EmailAddress)]
[Display(ResourceType = typeof (Resource), Name = "Email")]
[Required(ErrorMessageResourceName = "Error_EmailRequired", ErrorMessageResourceType = typeof(Resource))]
[RegularExpression(".+@.+", ErrorMessageResourceName = "Error_EmailWrong", ErrorMessageResourceType = typeof(Resource))]
public string Email { get; set; }
[Display(ResourceType = typeof(Resource), Name = "Topic")]
[Required(ErrorMessageResourceName = "Error_TopicRequired", ErrorMessageResourceType = typeof(Resource))]
public string Topic { get; set; }
[Display(ResourceType = typeof(Resource), Name = "Massage")]
[DataType(DataType.MultilineText)]
[Required(ErrorMessageResourceName = "Error_MassageRequired", ErrorMessageResourceType = typeof(Resource))]
public string Massage { get; set; }
}
消息视图:
@model *******.Models.EmailModel
@{
Layout = null;
}
To: @Model.To
Subject: @Model.Topic
The massage from @Model.Name @Model.Email
Massage text:
@Model.Massage
UnitTest:
public class EmailSetter
{
public const string DefaultName = "NameTest";
public const string DefaultEmail = "EmailTest@domaintest.test";
public const string DefaultTopic = "TopicTest";
public const string DefaultMassage = "Massage test.";
public EmailModel GetEmail(string Name = DefaultName, string Topic = DefaultTopic, string Email = DefaultEmail, string Massage = DefaultMassage)
{
EmailModel email = new EmailModel();
email.Name = Name;
email.Topic = Topic;
email.Email = Email;
email.Massage = Massage;
return email;
}
}
[TestClass]
public class MailTests
{
[TestMethod]
public void MailSendWithRightModel()
{
//Arrange
HomeController controller = new HomeController();
Mock<EmailModel> email = new Mock<EmailModel>();
email.Object.Name = EmailSetter.DefaultName;
email.Object.Email = EmailSetter.DefaultName;
email.Object.Topic = EmailSetter.DefaultTopic;
email.Object.Massage = EmailSetter.DefaultMassage;
//Act
controller.Contact(email.Object);
//Assert
email.Verify(mail => mail.Send());
}
}
来自测试的控制器:
[HttpPost]
[ValidateAntiForgeryToken]
public ActionResult Contact(EmailModel emailModel)
{
if(ModelState.IsValid)
{
ViewBag.Send = true;
emailModel.Send();
ModelState.Clear();
}
return View(emailModel);
}
我尝试过(在单元测试方法中):
通过new HttpBrowserCapabilities
new
BrowserCapabilitiesFactory
使用new
HttpBrowserCapabilitiesWrapper
创建HttpBrowserCapabilities
对象
任何想法如何使代码不抛出异常? (即使当前的HttpBrowserCapabilitiesWrapper获取现有的HttpBrowserCapabilities):)
答案 0 :(得分:1)
使用Postal的单元测试代码时,您可能需要验证 将发送一封电子邮件,而无需实际发送。
Postal提供了一个接口
IEmailService
和一个实现,EmailService
,实际上发送电子邮件。假设您使用某种IoC容器,请将其配置为注入
IEmailService
进入您的控制器。然后使用该服务发送电子邮件对象(而不是调用
Email.Send()
)。
public class HomeController : Controller {
readonly IEmailService emailService;
public HomeController(IEmailService emailService) {
this.emailService = emailService;
}
[HttpPost]
[ValidateAntiForgeryToken]
public ActionResult Contact(EmailModel email) {
if(ModelState.IsValid) {
emailService.Send(email);
ViewBag.Send = true;
ModelState.Clear();
}
return View(email);
}
}
通过创建
IEmailService
的模拟来测试此控制器 接口
[TestClass]
public class MailTests {
[TestMethod]
public void MailSendWithRightModel() {
//Arrange
var mockService = new Mock<IEmailService>();
var controller = new HomeController(mockService.Object);
var email = new EmailSetter().GetEmail();
//Act
controller.Contact(email);
//Assert
mockService.Verify(m => m.Send(email));
}
}