예제 #1
0
    public void Create_ForSupportedType_ReturnsBinder()
    {
        // Arrange
        var provider = new ComplexObjectModelBinderProvider();

        var context = new TestModelBinderProviderContext(typeof(Person));

        context.OnCreatingBinder(m =>
        {
            if (m.ModelType == typeof(int) || m.ModelType == typeof(string))
            {
                return(Mock.Of <IModelBinder>());
            }
            else
            {
                Assert.False(true, "Not the right model type");
                return(null);
            }
        });

        // Act
        var result = provider.GetBinder(context);

        // Assert
        Assert.IsType <ComplexObjectModelBinder>(result);
    }
        public IModelBinder GetBinder(ModelBinderProviderContext context)
        {
            var bodyBinder    = _bodyModelBinderProvider.GetBinder(context);
            var complexBinder = _complexTypeModelBinderProvider.GetBinder(context);

            if (context.BindingInfo.BindingSource != null && context.BindingInfo.BindingSource.CanAcceptDataFrom(BindingSource.Body))
            {
                return(new BodyAndRouteModelBinder(bodyBinder, complexBinder));
            }


            return(null);
        }
예제 #3
0
    public void Create_ForNonComplexType_ReturnsNull(Type modelType)
    {
        // Arrange
        var provider = new ComplexObjectModelBinderProvider();

        var context = new TestModelBinderProviderContext(modelType);

        // Act
        var result = provider.GetBinder(context);

        // Assert
        Assert.Null(result);
    }