public void Throw_exception_if_existsin_was_initialised_with_null_name_or_value_properties(string valueProperty, string nameProperty)
        {
            const string listProperty = "List";
            var vm = new ViewModelExample();
            var validationContext = new ValidationContext(vm, null, null);
            var attribute = new ExistsInAttribute(listProperty, valueProperty, nameProperty);

            Assert.Throws<ArgumentException>(
                () => attribute.GetValidationResult(vm.ListId, validationContext),
                "ExistsIn: You must pass valid properties for Name and Value to ExistsIn"
            );
        }
        public void Throw_exception_if_list_to_validate_against_does_not_exist()
        {
            const string valueProperty = "Id";
            const string nameProperty = "Name";
            const string listProperty = "List2";
            const string memberName = "MyListInput";
            var vm = new ViewModelExample();
            var validationContext = new ValidationContext(vm, null, null) { MemberName = memberName };
            var attribute = new ExistsInAttribute(listProperty, valueProperty, nameProperty);

            var ex = Assert.Throws<ArgumentException>(() => attribute.GetValidationResult(vm.ListId, validationContext));
            Assert.That(ex.Message, Is.EqualTo(string.Format("ExistsIn: No property Model.{0} exists for looking up values for property Model.{1}.", listProperty, memberName)));
        }
        public void Throw_exception_if_list_to_validate_against_does_not_contain_specified_name_property()
        {
            const string valueProperty = "Id";
            const string nameProperty = "Name2";
            const string listProperty = "List";
            var vm = new ViewModelExample();
            var validationContext = new ValidationContext(vm, null, null) { MemberName = listProperty };
            var attribute = new ExistsInAttribute(listProperty, valueProperty, nameProperty);
            var collectionType = vm.GetType().GetProperty(listProperty).PropertyType.GetGenericArguments().FirstOrDefault();

            var ex = Assert.Throws<ArgumentException>(() => attribute.GetValidationResult(vm.ListId, validationContext));
            Assert.That(ex.Message,
                Is.EqualTo(string.Format("ExistsIn: No property {0} exists for type {1} to look up possible values for property Model.{2}.", nameProperty, collectionType.Name, listProperty)
            ));
        }
 public ActionResult Form1(ViewModelExample vm)
 {
     return View(vm);
 }
 public ActionResult Form1(ViewModelExample vm)
 {
     return(View(vm));
 }
示例#6
0
 public ActionResult Partials(ViewModelExample vm)
 {
     return(View(vm));
 }
 public ActionResult Partials(ViewModelExample vm)
 {
     return View(vm);
 }
        public void Throw_exception_if_property_to_validate_against_is_not_an_ienumerable()
        {
            const string valueProperty = "Id";
            const string nameProperty = "Name";
            const string listProperty = "Decimal";
            var vm = new ViewModelExample();
            var validationContext = new ValidationContext(vm, null, null);
            var attribute = new ExistsInAttribute(listProperty, valueProperty, nameProperty);

            Assert.Throws<ArgumentException>(
                () => attribute.GetValidationResult(vm.ListId, validationContext),
                string.Format("ExistsIn: No property Model.{0} exists for validation.", valueProperty)
            );
        }
        public void Throw_exception_if_list_to_validate_against_is_null()
        {
            const string valueProperty = "Id";
            const string nameProperty = "Name";
            const string listProperty = "List";
            const string memberName = "MyListInput";
            var vm = new ViewModelExample
            {
                List = null
            };

            var validationContext = new ValidationContext(vm, null, null) { MemberName = memberName };
            var attribute = new ExistsInAttribute(listProperty, valueProperty, nameProperty);

            var ex = Assert.Throws<ListPropertyNullException>(() => attribute.GetValidationResult(vm.ListId, validationContext));
            Assert.That(ex.Message, Is.EqualTo(
                string.Format("The list property ({0}) specified in the [ExistsIn] on {1} is null.", listProperty, memberName)
            ));
        }