How do I Unit Test Actions without Mocking that use UpdateModel?

后端 未结 3 1617
误落风尘
误落风尘 2020-12-16 04:08

I have been working my way through Scott Guthrie\'s excellent post on ASP.NET MVC Beta 1. In it he shows the improvements made to the UpdateModel method and how they improv

相关标签:
3条回答
  • 2020-12-16 04:22

    Or you can create form data proxy, like

    public class CountryEdit {
      public String Name { get; set; }
      public String Iso3166 { get; set; }
    }
    
    • Plus. Easy create unit tests
    • Plus. Define white list of fields update from post
    • Plus. Easy setup validation rules, easy test it.
    • Minus. You should move date from proxy to you model

    So Controller.Action should look, like

    public ActionResult Edit(Int32 id, CountryEdit input)
    {
      var Country = input.ToDb();
      // Continue your code
    }
    
    0 讨论(0)
  • 2020-12-16 04:44

    I don't think it can be done since TryUpdateModel, which UpdateModel uses, references the ControllerContext which is null when invoked from a unit test. I use RhinoMocks to mock or stub the various components needed by the controller.

    var routeData = new RouteData();
    var httpContext = MockRepository.GenerateStub<HttpContextBase>();
    FormCollection formParameters = new FormCollection();
    
    EventController controller = new EventController();
    ControllerContext controllerContext = 
        MockRepository.GenerateStub<ControllerContext>( httpContext,
                                                        routeData,
                                                        controller );
    controller.ControllerContext = controllerContext;
    
    ViewResult result = controller.Create( formParameters ) as ViewResult;
    
    Assert.AreEqual( "Event", result.Values["controller"] );
    Assert.AreEqual( "Show", result.Values["action"] );
    Assert.AreEqual( 0, result.Values["id"] );
    

    Here's the relevant bit from the Controller.cs source on www.codeplex.com/aspnet:

    protected internal bool TryUpdateModel<TModel>( ... ) where TModel : class
    {
    
         ....
    
        ModelBindingContext bindingContext =
               new ModelBindingContext( ControllerContext,
                                        valueProvider,
                                        typeof(TModel),
                                        prefix,
                                        () => model,
                                        ModelState,
                                        propertyFilter );
    
         ...
    }
    
    0 讨论(0)
  • 2020-12-16 04:47

    I was having this same issue. After reading tvanfosson's solution, I tried a simple solution not involving a mock framework.

    Add a default ControllerContext to the controller as follows:

    CountryController controller = new CountryController();
    controller.ControllerContext = new ControllerContext();
    

    This removed the error just fine for me while unit testing. I hope this may help someone else out.

    0 讨论(0)
提交回复
热议问题