小编Mik*_* G.的帖子

ASP.NET MVC 2中的单元测试自定义模型绑定器

我在项目中编写了自定义模型绑定器,它使用ASP.NET MVC 2.此模型绑定器仅绑定2个模型字段:

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);
    }
}
Run Code Online (Sandbox Code Playgroud)

如何使用标准VS单元测试来测试此模型绑定器?花了几个小时谷歌搜索,找到几个例子( …

asp.net-mvc unit-testing modelbinders

21
推荐指数
1
解决办法
5122
查看次数

标签 统计

asp.net-mvc ×1

modelbinders ×1

unit-testing ×1