public void CustomObjectListInvalidPropertyName() { // see DemoItems collection decorated with a propertyname that doesn't exists in the CustomListObject object ListBaseDemoObject obj = new ListBaseDemoObject(); obj.DemoItems.Add(new CustomListObject() { DemoText = "ptv", LuckyNumber = 7 }); obj.DemoItems.Add(new CustomListObject() { DemoText = "ptv", LuckyNumber = 17 }); ValidationContext ctx = new ValidationContext(obj); ctx.MemberName = "DemoItems"; // notice the * usage at the end of the message Action act = () => Validator.ValidateProperty(obj.DemoItems, ctx); act.ShouldThrowExactly <ArgumentException>().WithMessage("Item doesn't contain property named: 'InvalidPropertyName'.*"); }
public void InvalidStringValuesInListProperty(string itemValue) { ListBaseDemoObject obj = new ListBaseDemoObject(); obj.MyInValidStrings.Add(itemValue); ValidationContext ctx = new ValidationContext(obj); ctx.MemberName = "MyInValidStrings"; Action act = () => Validator.ValidateProperty(obj.MyInValidStrings, ctx); act.ShouldThrowExactly <ValidationException>(); }
public void ValidatorUsedOnInvalidProperty() { ListBaseDemoObject obj = new ListBaseDemoObject(); // we need to set this value to other than null so that the logic tries to cast the property to IList obj.InvalidAttributeUsage = "diipadaapa"; ValidationContext ctx = new ValidationContext(obj); ctx.MemberName = "InvalidAttributeUsage"; Action act = () => Validator.ValidateProperty(obj.InvalidAttributeUsage, ctx); act.ShouldThrowExactly <InvalidOperationException>("ListBaseAttribute based attribute used on a property that doesn't implement IList."); }
public void CustomObjectListWithNullValue() { ListBaseDemoObject obj = new ListBaseDemoObject(); obj.CustomObjectList.Add(new CustomListObject() { DemoText = "ptv", LuckyNumber = 7 }); obj.CustomObjectList.Add(null); // add null item to the list ValidationContext ctx = new ValidationContext(obj); ctx.MemberName = "CustomObjectList"; Action act = () => Validator.ValidateProperty(obj.CustomObjectList, ctx); act.ShouldThrowExactly <ValidationException>().WithMessage("List contains a null object and propertyName is defined. Cannot get property value from null object."); }