簡體   English   中英

如何在ASP.NET MVC中測試自定義模型綁定器?

[英]How to test custom Model Binders in ASP.NET MVC?

我在ASP.NET MVC應用程序中編寫了一些自定義模型綁定器(實現IModelBinder)。 我想知道對它們進行單元測試的好方法(粘合劑)?

我是這樣做的:

var formElements = new NameValueCollection() { {"FirstName","Bubba"}, {"MiddleName", ""}, {"LastName", "Gump"} };         
var fakeController = GetControllerContext(formElements);
var valueProvider = new Mock<IValueProvider>();           

var bindingContext = new ModelBindingContext(fakeController, valueProvider.Object, typeof(Guid), null, null, null, null);



private static ControllerContext GetControllerContext(NameValueCollection form) {
    Mock<HttpRequestBase> mockRequest = new Mock<HttpRequestBase>();
    mockRequest.Expect(r => r.Form).Returns(form);

    Mock<HttpContextBase> mockHttpContext = new Mock<HttpContextBase>();
    mockHttpContext.Expect(c => c.Request).Returns(mockRequest.Object);

    return new ControllerContext(mockHttpContext.Object, new RouteData(), new Mock<ControllerBase>().Object);
}

然后我將bindingContext變量傳遞給實現IModelBinder接口的對象的BindModel方法。

這是我在博客上為您編寫的一種簡單的無模式方法,假設您使用的是ValueProvider而不是HttpContext: http//www.hanselman.com/blog/SplittingDateTimeUnitTestingASPNETMVCCustomModelBinders.aspx

[TestMethod]  
public void DateTime_Can_Be_Pulled_Via_Provided_Month_Day_Year_Hour_Minute_Second_Alternate_Names()  
{  
    var dict = new ValueProviderDictionary(null) {   
            { "foo.month1", new ValueProviderResult("2","2",null) },  
            { "foo.day1", new ValueProviderResult("12", "12", null) },  
            { "foo.year1", new ValueProviderResult("1964", "1964", null) },  
            { "foo.hour1", new ValueProviderResult("13","13",null) },  
            { "foo.minute1", new ValueProviderResult("44", "44", null) },  
            { "foo.second1", new ValueProviderResult("01", "01", null) }  
        };  

    var bindingContext = new ModelBindingContext() { ModelName = "foo", ValueProvider = dict };  

    DateAndTimeModelBinder b = new DateAndTimeModelBinder() { Month = "month1", Day = "day1", Year = "year1", Hour = "hour1", Minute = "minute1", Second = "second1" };  

    DateTime result = (DateTime)b.BindModel(null, bindingContext);  
    Assert.AreEqual(DateTime.Parse("1964-02-12 13:44:01"), result);  
}  

dict可以像這樣重構

            FormCollection form = new FormCollection
                                  {
                                      { "month1", "2" },
                                      { "day1", "12" },
                                      { "year1", "1964" },
                                      { "hour1", "13" },
                                      { "minute1", "44" },
                                      { "second1", "01" }
                                  };

            var bindingContext = new ModelBindingContext() { ModelName = "foo", ValueProvider = form.ToValueProvider() };  

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM