コード例 #1
0
        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'.*");
        }
コード例 #2
0
        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>();
        }
コード例 #3
0
        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.");
        }
コード例 #4
0
        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.");
        }