ASP.net MVC Mock Context (Context)Code
Var fakeContext = new FakeControllerContext (controller, new NameValueCollection (), new NameValueCollection ());
Controller. ControllerContext = fakeContext;
Controller. Request. QueryString ["s"] = ""
Or:
Var controllerContext = new FakeControllerContext (controller,
New RouteData (), "", "", new string [] {}, new NameValueCollection (),
New NameValueCollection (), new HttpCookieCollection (), new System. Web. SessionState. SessionStateItemCollection ());
Controller. ControllerContext = controllerContext;
If you want to access Request [""], add:
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
Used in BFA project BFA. Presentation. Impl. Test. BuyerCompanyControllerTest. AjaxPartialManagementReturnExpectWhenOnDefault ()
Download: MvcFakes.rar