?????????HttpPost?????????д??????????е???????

????????????????????

<SPAN style="WHITE-SPACE: pre"> </SPAN>[HttpPost]
        public ActionResult NewName()
        {
            ViewBag.Name = Request.Form["Name"];
            return View();
        }

????????????View???????@Html.Editor("Name")?????

??????????????????????moq??????????

<PRE class=html name="code"><SPAN style="WHITE-SPACE: pre"> </SPAN>[TestMethod()]
        public void NewNameTest()
        {</PRE><PRE class=html name="code"><SPAN style="WHITE-SPACE: pre"> </SPAN>    // 1?????Form????????????????о?
            FormCollection form = new FormCollection();
            form["Name"] = "zhong";

</PRE><PRE class=html name="code"><SPAN style="WHITE-SPACE: pre"> </SPAN>    // 2??mock Request????
            var mockRequest = new Mock<HttpRequestBase>();          
            mockRequest.SetupGet(m => m.Form).Returns(form);<SPAN style="WHITE-SPACE: pre"> </SPAN>// ????Request??Form??getter????????????????form

</PRE><PRE class=html name="code"><SPAN style="WHITE-SPACE: pre"> </SPAN>    // 3??mock HttpContext????
            var mockContext = new Mock<HttpContextBase>();
            mockContext.SetupGet(m => m.Request).Returns(mockRequest.Object);  // ????HttpContext??Request???????????Request????
 

</PRE><PRE class=html name="code"><SPAN style="WHITE-SPACE: pre"> </SPAN>    // 4???????Controller????
            HomeController target = new HomeController();
            target.ControllerContext = new ControllerContext(mockContext.Object?? new RouteData()?? target); // ?????HttpContext??????Controller
          
            // 5??????????????
            ViewResult actual;
            actual = target.NewName() as ViewResult;

</PRE><PRE class=html name="code"><SPAN style="WHITE-SPACE: pre"> </SPAN>    // 6?????????
            Assert.AreEqual("zhong"?? actual.ViewBag.Name);

        }</PRE><BR>
<BR>
<P></P>
<PRE></PRE>
<BR>
<P></P>