I am trying to create a unit test to test an Entity Framework object. I found this link: https://stackoverflow.com/a/464626/2326 , but the check for me will never get false. I use data annotations in the properties of an entity object. To do this, I create a MetaData object to include annotations and annotate the entity object as follows:
[MetadataType(typeof(MyEntityObjectMetaData))] public partial class MyEntityObject { }
My validation annotations are as follows:
public class MyEntityObjectMetaData { [StringLength(8, ErrorMessage = "Invalid Length for myProperty.")] public String myProperty { get; set; } }
And the code for unit test:
[TestMethod] public void TestMethod1() { MyEntityObject myEntityObject = new MyEntityObject(); myEntityObject.myProperty = "1234567890"; var context = new ValidationContext(myEntityObject, null, null); var results = new List<ValidationResult>(); var actual = Validator.TryValidateObject(myEntityObject, context, results); var expected = false; Assert.AreEqual(expected, actual); }
I do not understand why object verification returns true if I have an invalid value for the property. Thanks for any help.
source share