在 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);
}
}
如何使用标准 VS 单元测试来测试此模型绑定程序?花了几个小时谷歌搜索,找到几个例子(http://www.hanselman.com/blog/SplittingDateTimeUnitTestingASPNETMVCCustomModelBinders .aspx),但此示例适用于 MVC1,在使用 MVC2 时不起作用。
我很感激你的帮助。
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);
}
}
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.
I appreciate your help.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我修改了 Hanselman 的 MVC 1 示例来测试 ASP.Net MVC 2 模型绑定程序。 。
I've modified Hanselman's MVC 1 example to test ASP.Net MVC 2 model binders...
一般的做法是创建一个模拟的ControllerContext、模拟的ModelBindingContext、模拟的PropertyDescriptor,然后调用该方法。
如果您的模型绑定器使用其他服务,就像您的一样(您正在使用 NHibernate?),那么您必须将它们抽象出来并提供它们的模拟。
单元测试代码将如下所示:
您在编写单元测试时遇到什么问题?
The general approach is to create a mock ControllerContext, mock ModelBindingContext, and mock PropertyDescriptor, and then call the method.
If your model binder uses other services, which it looks like yours does (you're using NHibernate?), then you'll have to abstract those out and provide mocks of them as well.
The unit test code will look something like this:
What problem(s) are you having writing the unit test?