在ASP.NET MVC 2单元测试自定义模型粘合剂 [英] Unit testing custom model binder in ASP.NET MVC 2

查看:122
本文介绍了在ASP.NET MVC 2单元测试自定义模型粘合剂的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经写了自定义的模型绑定,使用ASP.NET MVC 2.本模型绑定绑定仅有2模型的字段:

I've wrote custom model binder in project, that uses ASP.NET MVC 2. This model binder bind just 2 fields of model:

public class TaskFormBinder : DefaultModelBinder
{
    protected override void BindProperty(ControllerContext controllerContext,
        ModelBindingContext bindingContext,
        PropertyDescriptor propertyDescriptor)
    {           
        if (propertyDescriptor.Name == "Type")
        {
            var value = bindingContext.ValueProvider.GetValue("Type");
            var typeId = value.ConvertTo(typeof(int));
            TaskType foundedType;
            using (var nhSession = Domain.GetSession())
            {
                foundedType = nhSession.Get<TaskType>(typeId);
            }
            if (foundedType != null)
            {
                SetProperty(controllerContext, bindingContext, propertyDescriptor, foundedType);
            }
            else
            {
                AddModelBindError(bindingContext, propertyDescriptor);
            }
            return;
        }
        if (propertyDescriptor.Name == "Priority")
        { /* Other field binding ... */
            return;
        }
        base.BindProperty(controllerContext, bindingContext, propertyDescriptor);
    }
}

如何使用测试VS非标准单元测试这一模型的粘合剂?花了几个小时谷歌上搜索,发现几个例子(http://www.hanselman.com/blog/SplittingDateTimeUnitTestingASPNETMVCCustomModelBinders.aspx),但是这方面的例子是MVC1,并使用MVC2时不工作。

How can i test this model binder using standart VS unit testing? Spent some hours googling, find couple examples (http://www.hanselman.com/blog/SplittingDateTimeUnitTestingASPNETMVCCustomModelBinders.aspx), but this examples is for MVC1, and dont work when using MVC2.

我AP preciate你的帮助。

I appreciate your help.

推荐答案

我修改<一个href=\"http://www.hanselman.com/blog/SplittingDateTimeUnitTestingASPNETMVCCustomModelBinders.aspx\">Hanselman's MVC 1 的例子来测试ASP.Net MVC 2模型绑定...

I've modified Hanselman's MVC 1 example to test ASP.Net MVC 2 model binders...

[Test]
public void Date_Can_Be_Pulled_Via_Provided_Month_Day_Year()
{
    // Arrange
    var formCollection = new NameValueCollection { 
        { "foo.month", "2" },
        { "foo.day", "12" },
        { "foo.year", "1964" }
    };

    var valueProvider = new NameValueCollectionValueProvider(formCollection, null);
    var modelMetadata = ModelMetadataProviders.Current.GetMetadataForType(null, typeof(FwpUser));

    var bindingContext = new ModelBindingContext
    {
        ModelName = "foo",
        ValueProvider = valueProvider,
        ModelMetadata = modelMetadata
    };

    DateAndTimeModelBinder b = new DateAndTimeModelBinder { Month = "month", Day = "day", Year = "year" };
    ControllerContext controllerContext = new ControllerContext();

    // Act
    DateTime result = (DateTime)b.BindModel(controllerContext, bindingContext);

    // Assert
    Assert.AreEqual(DateTime.Parse("1964-02-12 12:00:00 am"), result);
}

这篇关于在ASP.NET MVC 2单元测试自定义模型粘合剂的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆