Exemplo n.º 1
0
        public void Bind_ViewModelArray_Index()
        {
            var provider = new ValueProvider
            {
                Parameters =
                {
                    new Parameter("user[1].FirstName", "jonas"),
                    new Parameter("user[0].FirstName", "Arne"),
                    new Parameter("user[0].Age",       "32"),
                    new Parameter("user[1].Age",       "23")
                }
            };
            var mapper = new ModelMapper();

            mapper.Clear();
            mapper.AddBinder(new PrimitiveModelBinder());
            mapper.AddBinder(new ClassBinder());
            var context = new ModelBinderContext(typeof(UserViewModel[]), "user", "", provider)
            {
                RootBinder = mapper
            };

            var binder = new ArrayModelBinder();
            var actual = (UserViewModel[])binder.Bind(context);

            Assert.NotNull(actual);
            Assert.Equal("jonas", actual[1].FirstName);
            Assert.Equal("Arne", actual[0].FirstName);
            Assert.Equal(23, actual[1].Age);
            Assert.Equal(32, actual[0].Age);
        }
Exemplo n.º 2
0
        public void Bind_ViewModelArray_Gap()
        {
            var provider = new ValueProvider
            {
                Parameters =
                {
                    new Parameter("user[1].FirstName", "jonas"),
                    new Parameter("user[3].FirstName", "Arne"),
                    new Parameter("user[0].Age",       "32"),
                    new Parameter("user[1].Age",       "23")
                }
            };
            var mapper = new ModelMapper();

            mapper.Clear();
            mapper.AddBinder(new PrimitiveModelBinder());
            mapper.AddBinder(new ClassBinder());
            var context = new ModelBinderContext(typeof(UserViewModel[]), "user", "", provider)
            {
                RootBinder = mapper
            };

            var binder = new ArrayModelBinder();

            Assert.Throws <ModelBindingException>(() => binder.Bind(context));
        }
Exemplo n.º 3
0
        public void NoPublicDefaultConstructor()
        {
            var provider = new ValueProvider
            {
                Parameters =
                {
                    new Parameter("user.FirstName", "jonas"),
                }
            };
            var mapper = new ModelMapper();

            mapper.Clear();
            mapper.AddBinder(new PrimitiveModelBinder());
            var context = new ModelBinderContext(typeof(Test), "user", "", provider)
            {
                RootBinder = mapper
            };

            var binder = new ClassBinder();

            Assert.Throws <ModelBindingException>(() => binder.Bind(context));
        }
Exemplo n.º 4
0
        public void InvalidFieldType()
        {
            var provider = new ValueProvider
            {
                Parameters =
                {
                    new Parameter("user.FirstName", "jonas"),
                    new Parameter("user.Age",       "arne")
                }
            };
            var mapper = new ModelMapper();

            mapper.Clear();
            mapper.AddBinder(new PrimitiveModelBinder());
            var context = new ModelBinderContext(typeof(UserViewModel), "user", "", provider)
            {
                RootBinder = mapper
            };

            var binder = new ClassBinder();

            Assert.Throws <ModelBindingException>(() => binder.Bind(context));
        }
Exemplo n.º 5
0
        public void UnknownField()
        {
            var provider = new ValueProvider
            {
                Parameters =
                {
                    new Parameter("user.FirstName",       "jonas"),
                    new Parameter("user.NotInventedHere", "23")
                }
            };
            var mapper = new ModelMapper();

            mapper.Clear();
            mapper.AddBinder(new PrimitiveModelBinder());
            var context = new ModelBinderContext(typeof(UserViewModel), "user", "", provider)
            {
                RootBinder = mapper
            };

            var binder = new ClassBinder();
            var actual = (UserViewModel)binder.Bind(context);

            Assert.Equal("jonas", actual.FirstName);
        }