I'm working on a ASP.NET MVC 2 application and i test my Viewmodels Like this
[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));
}
The Email Property is required and this testmethod works fine
My Question is now: How can i test a MetaData Class like in the test before? the validation results are always true. Or isn't this possible?
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; }
}
}