System.ArgumentNullException
テストから呼び出されSystem.Web.HttpBrowserCapabilities
たときにコンストラクターからスローされます。パラメータmail.Send()
が必要です。httpBrowserCapabilities
私が使用したコード:
メールモデル:
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
単体テスト :
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 を取得させる) :)