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

后端 未结 3 1730
误落风尘
误落风尘 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
    }
    

提交回复
热议问题