ASP.net MVC Mock Context(上下文)Code
var fakeContext = new FakeControllerContext(controller, new NameValueCollection(), new NameValueCollection());
controller.ControllerContext = fakeContext;
controller.Request.QueryString["s"] = ""
或:
var controllerContext = new FakeControllerContext(controller,
new RouteData(), "", "", new string[] { }, new NameValueCollection(),
new NameValueCollection(), new HttpCookieCollection(), new System.Web.SessionState.SessionStateItemCollection());
controller.ControllerContext = controllerContext;
如果要訪問Request[""],則加上:
var mockRequst = new Mock<HttpRequestBase>();
mockRequst.ExpectGet(r => r.ServerVariables).Returns(new NameValueCollection());
mockRequst.ExpectGet(r => r.QueryString).Returns(new NameValueCollection());
mockRequst.ExpectGet(r => r.Form).Returns(new NameValueCollection());
mockRequst.ExpectGet(r => r.Cookies).Returns(new HttpCookieCollection());
var mockHttpContext = new Mock<HttpContextBase>();
mockHttpContext.ExpectGet(hc => hc.Request).Returns(mockRequst.Object);
controller.ControllerContext.HttpContext = mockHttpContext.Object;
namespace MvcFakes
在 BFA項目BFA.Presentation.Impl.Test.BuyerCompanyControllerTest.AjaxPartialManagementReturnExpectWhenOnDefault()用到
下載:MvcFakes.rar