How do I test DataType.EmailAddress
?
I have a Customer
model with an Email
property with the following data annotations to validate:
[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; }
I am writing unit tests to test validations. I have figured out how to test required and string length.
Here's my method that catches other errors, but doesn't handle the DataType validations:
private List<ValidationResult> ValidateModel<T>(T model)
{
var context = new ValidationContext(model, null, null);
var result = new List<ValidationResult>();
var valid = Validator.TryValidateObject(model, context, result, true);
return result;
}
I call it in a test method:
[TestMethod]
public void Invalid_email_addresses_throw_errors()
{
var model = new Models.Customer();
model.email = "";
var results = ValidateModel(model);
Assert.IsTrue(results.Any(v => v.ErrorMessage == "Email is required"));
}
How do I test DataType.EmailAddress
- passing in an invalid value and receiving the an error as a result?
The following Model class consists of one property Email to which the following validation Data Annotation attributes have been applied. 1. Required Data Annotation attribute. 2. EmailAddress Data Annotation attribute.
ASP.NET MVC - Data Annotations 1 Key. Entity Framework relies on every entity having a key value that it uses for tracking entities. ... 2 Timestamp. ... 3 ConcurrencyCheck. ... 4 Required. ... 5 MaxLength. ... 6 MinLength. ... 7 StringLength. ... 8 Table. ... 9 Column. ... 10 Index. ... More items...
DataType attribute is used for formatting purposes, not for validation. I suggest you use ASP.NET MVC 3 Futures for email validation. [Required] [DataType (DataType.EmailAddress)] [EmailAddress] public string Email { get; set; }
.NET 4.5 introduced the [EmailAddress] attribute, a subclass of DataTypeAttribute. By using [EmailAddress] you get both client and server side validation. Show activity on this post.
Use the [EmailAddress]
DataTypeAttribute.
EmailAddressAttribute is derived from DataTypeAttribute
and overrides the IsValid
method which is what checks that the value is in fact a valid email.
Using
[DataType(DataType.EmailAddress, ErrorMessage = "Email must be a valid email address")]
does not do anything for email validation.
If you inspect the source code for DataTypeAttribute you will realize that it is primarily a base attribute used to create custom and targeted validation attribute.
The DataTypeAttribute
in the original question is being used incorrectly.
There is no other solution than to use the EmailAddressAttribute
as demonstrated below in the following unit test.
[TestClass]
public class UnitTestExample {
[TestMethod]
public void Invalid_email_addresses_throw_errors() {
//Arrange
var badEmail = "1234_)(";
var subject = new Customer { email = badEmail };
//Act
var results = ValidateModel(subject);
//Assert
Assert.IsTrue(results.Count > 0);
Assert.IsTrue(results.Any(v => v.MemberNames.Contains("email")));
}
public class Customer {
[StringLength(100)]
[Display(Name = "Email")]
[Required(ErrorMessage = "Email is required")]
[EmailAddress(ErrorMessage = "Email must be a valid email address")]
public string email { get; set; }
}
private List<ValidationResult> ValidateModel<T>(T model) {
var context = new ValidationContext(model, null, null);
var result = new List<ValidationResult>();
var valid = Validator.TryValidateObject(model, context, result, true);
return result;
}
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With