asp.net mvc3??HttPost?????????moq???е??????
???????????? ???????[ 2013/7/29 13:59:53 ] ????????
?????????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>
??????
???·???
??????????????????
2023/3/23 14:23:39???д?ò??????????
2023/3/22 16:17:39????????????????????Щ??
2022/6/14 16:14:27??????????????????????????
2021/10/18 15:37:44???????????????
2021/9/17 15:19:29???·???????·
2021/9/14 15:42:25?????????????
2021/5/28 17:25:47??????APP??????????
2021/5/8 17:01:11