【发布时间】:2010-01-02 19:50:17
【问题描述】:
我在项目中编写了自定义模型绑定器,它使用 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);
}
}
如何使用标准 VS 单元测试来测试这个模型绑定器?花了几个小时在谷歌上搜索,找到了几个示例 (http://www.hanselman.com/blog/SplittingDateTimeUnitTestingASPNETMVCCustomModelBinders.aspx),但这个示例适用于 MVC1,在使用 MVC2 时不起作用。
感谢您的帮助。
【问题讨论】:
标签: asp.net-mvc unit-testing modelbinders