【问题标题】:asp.net mvc and linq to entities: how to test model custom binding?asp.net mvc 和 linq to entity:如何测试模型自定义绑定?
【发布时间】:2009-11-30 17:25:09
【问题描述】:

我正在尝试为我的自定义模型绑定器构建测试,但没有任何成功。对 base.BindModel 的调用始终返回 null。使用 LINQ to Entities 时是否可以测试自定义绑定?在这种情况下,foo 是 db 中的一个表,其中包含两个字段 - 一个数字和一个文本值。

fooBinder.cs:

public override Object BindModel(ControllerContext controllerContext, ModelBindingContext bindingContext)
{
  var obj = (foo)base.BindModel(controllerContext, bindingContext);
  return obj;
}

我正在尝试做一个简单的测试(这里有一些额外的碎片,我尝试了许多不同的方法):

fooBinderTest.cs:

fooBinder binder;
ControllerContext controllerContext;
ModelBindingContext bindingContext;

[TestInitialize]
public void Initialize()
{
  controllerContext = MockControllerContext().Object;

  FormCollection form = new FormCollection
      {
        {"foo_A","2" },
        {"foo_B", "FooB" }
      };
  var valueProvider = form.ToValueProvider();

  bindingContext = new ModelBindingContext()
  {
    ModelState = new ModelStateDictionary(),
    ModelType = typeof(foo),
    ModelName = "foo",
    ValueProvider = valueProvider
  };

  binder = new fooBinder();

}

public static Mock<ControllerContext> MockControllerContext()
{
  var context = new Mock<ControllerContext>();
  var sessionState = new Mock<HttpSessionStateBase>();
  var response = new Mock<HttpResponseBase>();
  var request = new Mock<HttpRequestBase>();
  var serverUtility = new Mock<HttpServerUtilityBase>();

  var form = new FormCollection
        {
        {"foo_A","2" },
        {"foo_B", "FooB" }
        };

  context.Setup(c => c.HttpContext.Session).Returns(sessionState.Object);
  context.Setup(c => c.HttpContext.Response).Returns(response.Object);
  context.Setup(c => c.HttpContext.Request).Returns(request.Object);
  context.Setup(c => c.HttpContext.Server).Returns(serverUtility.Object);
  context.Setup(c => c.HttpContext.User.Identity.Name).Returns("Test");
  context.Setup(c => c.HttpContext.Request.Form).Returns(form);

  return context;
}


[TestMethod]
public void TestDefault()
{
  foo myFoo = (foo)binder.BindModel(controllerContext, bindingContext);
  Assert.IsNotNull(myFoo);
}

【问题讨论】:

    标签: asp.net-mvc unit-testing binding linq-to-entities


    【解决方案1】:

    L2E 在这里是一个红鲱鱼。

    您必须指定您的自定义模型绑定器,因为您没有在测试中运行 MVC 基础架构。

    我是这样做的(MVC 2;1 有点不同)。

        internal static T Bind<T>(string prefix, FormCollection collection, ModelStateDictionary modelState)
        {
            var mbc = new ModelBindingContext()
            {
                ModelMetadata = ModelMetadataProviders.Current.GetMetadataForType(null, typeof(T)),
                ModelName = prefix,
                ModelState = modelState,
                ValueProvider = collection.ToValueProvider()
            };
            IModelBinder binder = new MyCustomModelModelBinder();
            var cc = new ControllerContext();
    
            return binder.BindModel(cc, mbc) as T;
        }
    
        internal static T BindAndAssertValid<T>(string prefix, FormCollection collection)
        {
            var msd = new ModelStateDictionary();
            var result = Bind<T>(prefix, collection, msd);
            if (!msd.IsValid)
            {
                Assert.Fail(ModelStateValidationSummary(msd));
            }
            return result;
        }
    

    【讨论】:

    • 不幸的是,我们使用的是 MVC 1。
    • 你可以在 MVC 1 上做几乎完全相同的事情;只有您在构造 ModelBindingContext 时设置的属性会发生变化。
    • IIRC 你设置了Model 而不是ModelMetadata。但已经有一段时间了。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2010-12-20
    • 1970-01-01
    相关资源
    最近更新 更多