How do I make fun of HttpContext in ASP.NET MVC using Moq?

[TestMethod] public void Home_Message_Display_Unknown_User_when_coockie_does_not_exist() { var context = new Mock<HttpContextBase>(); var request = new Mock<HttpRequestBase>(); context .Setup(c => c.Request) .Returns(request.Object); HomeController controller = new HomeController(); controller.HttpContext = context; //Here I am getting an error (read only). ... } 

my base controller has an initialization override that receives this request. I am trying to convey this, but I am not doing anything right.

 protected override void Initialize(System.Web.Routing.RequestContext requestContext) { base.Initialize(requestContext); } 

Where can I get more information about taunting my RequestContext and HttpContext using Moq? I am trying to make fun of cookies and the general context.

+90
c # asp.net-mvc moq mocking
Sep 21 '09 at 0:24
source share
5 answers

The HttpContext is read-only, but it is actually derived from the ControllerContext that you can set.

  controller.ControllerContext = new ControllerContext( context.Object, new RouteData(), controller ); 
+52
Sep 21 '09 at 1:05
source

Create a request, response and put them in an HttpContext:

 HttpRequest httpRequest = new HttpRequest("", "http://mySomething/", ""); StringWriter stringWriter = new StringWriter(); HttpResponse httpResponse = new HttpResponse(stringWriter); HttpContext httpContextMock = new HttpContext(httpRequest, httpResponse); 
+33
Sep 21 '09 at 2:30 p.m.
source

Thanks to user 0100110010101.

This worked for me, and here I had a problem writing test code for the code below:

  var currentUrl = Request.Url.AbsoluteUri; 

And here are the lines that solved the problem

 HomeController controller = new HomeController(); //Mock Request.Url.AbsoluteUri HttpRequest httpRequest = new HttpRequest("", "http://mySomething", ""); StringWriter stringWriter = new StringWriter(); HttpResponse httpResponse = new HttpResponse(stringWriter); HttpContext httpContextMock = new HttpContext(httpRequest, httpResponse); controller.ControllerContext = new ControllerContext(new HttpContextWrapper(httpContextMock), new RouteData(), controller); 

May be useful for others.

+9
Jun 30 '15 at 12:02
source

Here is an example of how you can set this: Mocking HttpContext HttpRequest and HttpResponse for UnitTests (using Moq)

Check out extension methods that really help simplify the use of these mocking classes:

 var mockHttpContext = new API_Moq_HttpContext(); var httpContext = mockHttpContext.httpContext(); httpContext.request_Write("<html><body>".line()); httpContext.request_Write(" this is a web page".line()); httpContext.request_Write("</body></html>"); return httpContext.request_Read(); 

Here is an example of how to write Unit Test using moq to verify that the HttpModule is working as expected: Unit Test for HttpModule using Moq to wrap HttpRequest

Update: This API has been reorganized to

+5
Apr 10 '11 at 3:21
source

This is how I used ControllerContext to pass the false path of the application:

 [TestClass] public class ClassTest { private Mock<ControllerContext> mockControllerContext; private HomeController sut; [TestInitialize] public void TestInitialize() { mockControllerContext = new Mock<ControllerContext>(); sut = new HomeController(); } [TestCleanup] public void TestCleanup() { sut.Dispose(); mockControllerContext = null; } [TestMethod] public void Index_Should_Return_Default_View() { // Expectations mockControllerContext.SetupGet(x => x.HttpContext.Request.ApplicationPath) .Returns("/foo.com"); sut.ControllerContext = mockControllerContext.Object; // Act var failure = sut.Index(); // Assert Assert.IsInstanceOfType(failure, typeof(ViewResult), "Index() did not return expected ViewResult."); } } 
+4
Oct 27 2018-11-11T00:
source



All Articles