4

I am trying to create a unit test for the validation of an Entity Framework object. I found this link: https://stackoverflow.com/a/11514648/2486661 but the validation for me never get the value false. I am using data annotations in the properties of the entity object. For this I create a MetaData object for including the annotations and annotated the entity object like this:

[MetadataType(typeof(MyEntityObjectMetaData))]
public partial class MyEntityObject
{
}

My validation annotations are like this:

public class MyEntityObjectMetaData
{
    [StringLength(8, ErrorMessage = "Invalid Length for myProperty.")]
    public String myProperty { get; set; } 
}

And the code for the 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 the validation of the object return the value true if I have an invalid value for the property. Thanks for any help.

3 Answers 3

3

Here is an example of the code as part of a series of tests I run against each View Model, including a test to make sure the expected property names are there.

/// <summary>
/// Check expected properties exist.
/// </summary>
[Test]
public void Check_Expected_Properties_Exist()
{

// Get properties.
PropertyInfo propInfoFirstName = typeof(ViewModels.MyModel).GetProperty("FirstName");
PropertyInfo propInfoLastName = typeof(ViewModels.MyModel).GetProperty("LastName");

// Assert.
Assert.IsNotNull(propInfoFirstName);
Assert.IsNotNull(propInfoLastName);

}

Hope this helps.

Sign up to request clarification or add additional context in comments.

Comments

2

I resolve the problem using the DBContext object like this:

 MyEntityObject myEntityObject = new MyEntityObject();
 myEntityObject.myProperty = "1234567890";

var dbContext = new DbContext(MyEntityObject, true);

int errors = dbContext.GetValidationErrors().Count();

IEnumerable<DbEntityValidationResult> validationResults = 
                                         dbContext.GetValidationErrors();
DbValidationError validationError = validationResults.First().ValidationErrors.First();

Assert.AreEqual(1, errors);
Assert.AreEqual("myProperty", validationError.PropertyName);

MyEntityObject is a subclass of ObjectContext class and is auto generated by the Entity Framework. I still don't understand why using the Validator.TryValidateObject method doesn't work.

Comments

2

You have to tell the validator to validate all properties. The dbcontext will track changes to the entity and pass specific properties to the validator.

In EF 6 you would call

var actual = Validator.TryValidateObject(myEntityObject, context, results, validateAllProperties: true);

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.