コード例 #1
0
        public void CustomerPictureModelBinder_Returns_CustomerPicture_With_Photo()
        {
            //Arrange
            int customerId = 15;
            string photoAsString = Convert.ToBase64String(new byte[] { 0x10, 0x10, 0x01 });

            //Create a mock of the posted file.
            SHttpPostedFileBase customerPhoto = new SHttpPostedFileBase();
            customerPhoto.InputStreamGet = () => new MemoryStream(Convert.FromBase64String(photoAsString));
            customerPhoto.ContentLengthGet = () => photoAsString.Length;


            //Create the binder
            IValueProvider provider = new SIValueProvider()
            {
                //Mock the contains prefix string to return true only for the Photo and the CustomerId
                ContainsPrefixString = x => x.StartsWith("CustomerPicture.Photo") || x.StartsWith("CustomerPicture.CustomerId") || x.Equals("CustomerPicture"),
                //Mock the values returned for Photo and CustomerId.
                GetValueString = x =>
                {
                    switch (x)
                    {
                        case "CustomerPicture.Photo":
                            return new ValueProviderResult(customerPhoto, photoAsString, null);
                        case "CustomerPicture.CustomerId":
                            return new ValueProviderResult(15, "15", null);
                        default:
                            return null;
                    }
                }
            };

            
            ModelMetadata metadata = ModelMetadataProviders.Current.GetMetadataForType(null, typeof(CustomerPicture));
            
            CustomerPictureModelBinder binder = new CustomerPictureModelBinder();
            
            ControllerContext context = new ControllerContext();
            
            ModelBindingContext bindingContext = new ModelBindingContext()
            {
                ModelName = "CustomerPicture",
                ValueProvider = provider,
                ModelMetadata = metadata
            };

            //Act
            CustomerPicture result = binder.BindModel(context, bindingContext) as CustomerPicture;


            //Assert
            Assert.AreEqual(customerId, result.CustomerId);
            Assert.IsNull(result.Customer);
        }
コード例 #2
0
        public void CustomerPictureModelBinder_Returns_CustomerPicture_Without_Photo_When_There_Isnt_Photo()
        {
            //Arrange
            int customerId = 15;
            IValueProvider provider = new SIValueProvider()
            {
                
                ContainsPrefixString = x => x.StartsWith("CustomerPicture.CustomerId") || x.Equals("CustomerPicture"),
                GetValueString = x =>
                {
                    switch (x)
                    {
                        case "CustomerPicture.CustomerId":
                            return new ValueProviderResult(15, "15", null);
                        default:
                            return null;
                    }
                }
            };
            
            ModelMetadata metadata = ModelMetadataProviders.Current.GetMetadataForType(null, typeof(CustomerPicture));
            
            CustomerPictureModelBinder binder = new CustomerPictureModelBinder();
            
            ControllerContext context = new ControllerContext();
            
            ModelBindingContext bindingContext = new ModelBindingContext()
            {
                ModelName = "CustomerPicture",
                ValueProvider = provider,
                ModelMetadata = metadata
            };

            //Act
            CustomerPicture result = binder.BindModel(context, bindingContext) as CustomerPicture;

            //Assert
            Assert.AreEqual(customerId, result.CustomerId);
            Assert.IsNull(result.Customer);
            Assert.IsNull(result.Photo);
        }