title: Asp.net Unit test model data annotation DataType.EmailAddress?
body: <p>How do I test <code>DataType.EmailAddress</code>?</p>
  
  <p>I have a <code>Customer</code> model with an <code>Email</code> property with the following data annotations to validate:</p>
  
  <pre><code>[StringLength(100)]
  [DataType(DataType.EmailAddress, ErrorMessage = "Email must be a valid email address")]
  [Display(Name = "Email")]
  [Required(ErrorMessage = "Email is required")]
  public string email { get; set; }
  </code></pre>
  
  <p>I am writing unit tests to test validations. I have figured out how to test required and string length. </p>
  
  <p>Here's my method that catches other errors, but doesn't handle the DataType validations:</p>
  
  <pre><code>private List&lt;ValidationResult&gt; ValidateModel&lt;T&gt;(T model)
  {
      var context = new ValidationContext(model, null, null);
      var result = new List&lt;ValidationResult&gt;();
      var valid = Validator.TryValidateObject(model, context, result, true);
  
      return result;
  }
  </code></pre>
  
  <p>I call it in a test method:</p>
  
  <pre><code>[TestMethod]
  public void Invalid_email_addresses_throw_errors()
  {
      var model = new Models.Customer();
  
      model.email = "";
      var results = ValidateModel(model);
      Assert.IsTrue(results.Any(v =&gt; v.ErrorMessage == "Email is required"));
  }
  </code></pre>
  
  <p>How do I test <code>DataType.EmailAddress</code> - passing in an invalid value and receiving the an error as a result?</p>