2008-10-31 15 views

Respuesta

12

lo hice de esta manera:

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); 
} 

Y entonces me acaba de pasar en la variable BindingContext al método bindModel del objeto que implementa la interfaz IModelBinder.

+2

El contructor la sobrecarga utilizada para ModelBindingContext no parece existir. –

+0

Sí, solo uso el constructor sin parámetros y luego llamo a BindModel directamente con el ControllerContext falso. Parece que funciona hasta ahora. –

12

Aquí está una manera simple sin burla que escribí para ti en mi blog suponiendo que el uso del ValueProvider y no 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); 
} 
+4

Aquí está la [actualización MVC 2 de esta respuesta] (http://stackoverflow.com/questions/1992629/unit-testing-custom-model-binder-in-asp-net-mvc-2/2310954#2310954) para cualquier persona quien termina aquí antes de que lo encuentren – patridge

3

dict podría ser reprogramado como esto

  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() }; 
Cuestiones relacionadas