public void TryValidateObject_object_with_dictionary_does_not_fail() { var parent = new Parent { PropertyA = 1, PropertyB = 1 }; var classWithDictionary = new ClassWithDictionary { Objects = new List <Dictionary <string, Child> > { new Dictionary <string, Child> { { "key", new Child { Parent = parent, PropertyA = 1, PropertyB = 2 } } } } }; var validationResults = new List <ValidationResult>(); var result = _validator.TryValidateObjectRecursive(classWithDictionary, validationResults); Assert.IsTrue(result); Assert.IsEmpty(validationResults); }
public void TryValidateObject_object_with_dictionary_does_fail() { var parent = new Parent { PropertyA = 1, PropertyB = 1 }; var classWithDictionary = new ClassWithDictionary { DataList = new List <Dictionary <string, Child> > { new Dictionary <string, Child> { { "key0", new Child { Parent = parent, PropertyA = 1, PropertyB = 1 } }, { "key1", new Child { Parent = parent, PropertyA = 1, PropertyB = 10 } } } } }; var result = _validator.TryValidateObjectRecursive(classWithDictionary, out var validationResults); Assert.IsFalse(result); Assert.AreEqual(1, validationResults.Count); var results = new List <ValidationResult>(validationResults); var result1 = results[0]; Assert.AreEqual("Child PropertyA and PropertyB cannot add up to more than 10.", result1.ErrorMessage); Assert.AreEqual(1, result1.MemberNames.Count()); Assert.AreEqual(@"DataList[0][Index=1, Key=""key1""]", result1.MemberNames.First()); }