我使用的MVC2框架上asp.net的MVC应用程序的工作。

下面是图。

<% using (Ajax.BeginForm("CreateMenuCategory",
           "Admin", 
           new AjaxOptions { UpdateTargetId = "tabs-MenuCategories", }))
       { %>
       <fieldset class="fields">
           <legend>
                Add Menu Categories
            </legend>
            <p>
                <label for="MenuCategoryName">MenuCategory Name:</label>
                <%= Html.TextBox("MenuCategoryName")%>
                <%= Html.ValidationMessage("MenuCategoryName")%>
            </p>
            <p>
                <label for="Description">Description</label>
                <%= Html.TextBox("Description")%>
                 <%= Html.ValidationMessage("Description")%>
            </p>               
            <p>
                <label for="Notes">Notes</label>
                <%= Html.TextBox("Notes")%>
            </p>        
             <p class="submit">
                    <input type="submit" value="Create" />
             </p>       
    </fieldset>
    <% } %>

下面是我用于modelbinding类

public class MenuCategoryBusinessObject
    {


        //private readonly IMenuRepository _repository;
        public int ID { get; set; }

        [Required]
        [StringLength(20)]
        public string MenuCategoryName { get; set; }

        [Required]
        [StringLength(20)]
        public string Description { get; set; }

        public string Notes { get; set; }

        public IEnumerable<MenuItemBusinessObject> MenuItems
        {
            get; set;
        }

    }

这是我的控制。

 [HttpPost]
    public ActionResult CreateMenuCategory([Bind(Exclude = "ID")]MenuCategoryBusinessObject  menuCategory)
    {

        if(ModelState.IsValid)
        {//if I am valid.
            _repository.CreateMenuCategory(menuCategory);
        }

        IndexMenuCategory model = new IndexMenuCategory
        {
            MenuCategories = _repository.GetMenuCategories(),
            SelectedMenuCategory = null

        };
        return PartialView("MenuCategories", model);
    }

当我做模型结合,数据注释验证已经知道该模型是无效和ModelState.IsValid是假的。

然而,当我做单元测试,如果我养活我自己MenuCategoryBusinessObject到操作方法,它绕过了modelbinding,并不会知道的ModelState是无效的。

 [Fact]
    public void CreateNewMenuCategory()
    {
        // Setup
        DataStore dataStore = new DataStore();
        IMenuRepository menuRepository = new MenuRepository(dataStore);
        MenuCategoryBusinessObject menuCategoryBusinessObject =
            new MenuCategoryBusinessObject();
        AdminController adminControl = new AdminController(menuRepository);
        adminControl.SetFakeControllerContext();
        adminControl.Request.SetHttpMethodResult("POST");

        // Execute
        adminControl.CreateMenuCategory(menuCategoryBusinessObject);
    }

所以,我的问题是,如何在这种情况下,有效的单元测试?

有帮助吗?

解决方案

controllerInstance.ModelState.AddModelError("", "Dummy value.");

添加的伪值被称为方法之前将清除的IsValid 标记。然后,只需调用动作方法中测试该特定代码路径。

其他提示

为了测试是否该模型是正确的数据注解的予可能会做的线中的内容:

[TestMethod]
public void Description_Should_Be_Required()
{
    Expression<Func<MenuCategoryBusinessObject, object>> expression = 
        o => o.Description;
    var me = expression.Body as MemberExpression;
    var att = (RequiredAttribute[])me.Member
              .GetCustomAttributes(typeof(RequiredAttribute), false);
    att.Length.ShouldEqual(1);
}

您需要测试的另一件事是,你是如何有效使用的DataAnnotationsModelBinder而不是默认的:

ModelBinders.Binders.DefaultBinder = new DataAnnotationsModelBinder();
许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top