public void ModelBase_MultiFieldValidation() { var person = new SimplePerson(); person.FirstName = "Tom"; person.LastName = "Tom"; IList <ValidationResult> errors = person.GetErrors("FirstName"); Assert.AreEqual(1, errors.Count); Assert.IsTrue(errors[0].MemberNames.Contains("FirstName")); Assert.IsFalse(string.IsNullOrEmpty(errors[0].ErrorMessage)); errors = person.GetErrors("LastName"); Assert.AreEqual(1, errors.Count); Assert.IsTrue(errors[0].MemberNames.Contains("LastName")); Assert.IsFalse(string.IsNullOrEmpty(errors[0].ErrorMessage)); errors = ((INotifyDataErrorInfo)person).GetErrors("LastName").Cast <ValidationResult>().ToList(); Assert.AreEqual(1, errors.Count); Assert.IsTrue(errors[0].MemberNames.Contains("LastName")); Assert.IsFalse(string.IsNullOrEmpty(errors[0].ErrorMessage)); errors = person.GetErrors(); Assert.AreEqual(1, errors.Count); Assert.IsTrue(errors[0].MemberNames.Contains("FirstName")); Assert.IsTrue(errors[0].MemberNames.Contains("LastName")); Assert.IsFalse(string.IsNullOrEmpty(errors[0].ErrorMessage)); var interfacePerson = (IDataErrorInfo)person; Assert.IsTrue(!string.IsNullOrEmpty(interfacePerson.Error)); Assert.IsTrue(!string.IsNullOrEmpty(interfacePerson["FirstName"])); Assert.IsTrue(!string.IsNullOrEmpty(interfacePerson["LastName"])); }
public void ModelBase_ValidationTest2() { using (var verify = new Verify()) { var person = new SimplePerson(); var eventAssert = new PropertyChangedEventTest(verify, person); person.Validate(); Assert.IsTrue(person.HasErrors); eventAssert.ExpectEvent("HasErrors"); var errors = person.GetErrors("FirstName"); Assert.AreEqual(1, errors.Count); person.ClearErrors(); Assert.IsFalse(person.HasErrors); var errors2 = person.GetErrors("FirstName"); Assert.AreEqual(0, errors2.Count); } }
public void ModelBase_BasicValidation() { var person = new SimplePerson(); Assert.IsFalse(person.HasErrors); var errors = person.GetErrors(); Assert.AreEqual(0, errors.Count); errors = person.GetErrors(""); Assert.AreEqual(0, errors.Count); errors = person.GetErrors(null); Assert.AreEqual(0, errors.Count); person.Validate(); Assert.IsTrue(person.HasErrors); errors = person.GetErrors(); Assert.AreEqual(0, errors.Count); errors = person.GetErrors("FirstName"); Assert.AreEqual(1, errors.Count); Assert.AreEqual("FirstName", errors[0].MemberNames.First()); Assert.IsFalse(string.IsNullOrEmpty(errors[0].ErrorMessage)); #if !WINDOWS_UWP var interfacePerson = (IDataErrorInfo)person; Assert.IsFalse(!string.IsNullOrEmpty(interfacePerson.Error)); Assert.IsTrue(!string.IsNullOrEmpty(interfacePerson["FirstName"])); #endif person.FirstName = "Tom"; Assert.IsFalse(person.HasErrors); errors = person.GetErrors(); Assert.AreEqual(0, errors.Count); errors = person.GetErrors("FirstName"); Assert.AreEqual(0, errors.Count); #if !WINDOWS_UWP Assert.IsFalse(!string.IsNullOrEmpty(interfacePerson.Error)); Assert.IsFalse(!string.IsNullOrEmpty(interfacePerson["FirstName"])); #endif }