I am working on an ASP.NET MVC 2 application and I am testing my view models.
[TestMethod] public void EmailNeedsToBeSet() { var pvm = new ProfileViewModel {Email = ""}; var validationResults = new ValidationResults(); var result = Validator.TryValidateObject(pvm, new ValidationContext(pvm, null, null), validationResults, true); Assert.IsFalse(result); Assert.IsTrue(validationResults.ContainsField<ProfileViewModel>(m => m.Email)); }
An email property is required and this test method works great
My question is: how can I check the MetaData class, as in the test before? verification results are always true. Or is it not possible?
An example of my MetaData class:
[MetadataType(typeof (UserMetaData))] public partial class User { public class UserMetaData { [Required(ErrorMessageResourceName = "Required", ErrorMessageResourceType = typeof (ValidationStrings))] [LocalizedDisplayName("LoginName", NameResourceType = typeof (Fields))] [StringLength(64)] public string Login { get; set; } } }
source share