Setting properties for mocked HttpContextBase

I am working on an ASP.NET MVC application and am trying to write some unit tests against controller actions, some of which manipulate properties in an HttpContext such as Session, Request.Cookies, Response.Cookies, etc. I am having trouble figuring out how to "Arrange, Act, Assert" ... I can see Arrange and Assert ... but it's hard for me to figure out how to "act" on the properties of the mocked HttpContextBase when all its properties have only getters. I canโ€™t set anything in my mocked context from my actions with the controller ... so this is not very useful. I'm new to ridicule, so I'm sure something is missing there, but it seems logical that I can create a mock object that I can use in the context of testing controller actions, where I can actually set property values, and then later to claim that they are still what I installed them to, or something like that. What am I missing?

public static HttpContextBase GetMockHttpContext() { var requestCookies = new Mock<HttpCookieCollection>(); var request = new Mock<HttpRequestBase>(); request.Setup(r => r.Cookies).Returns(requestCookies.Object); request.Setup(r => r.Url).Returns(new Uri("http://example.org")); var responseCookies = new Mock<HttpCookieCollection>(); var response = new Mock<HttpResponseBase>(); response.Setup(r => r.Cookies).Returns(responseCookies.Object); var context = new Mock<HttpContextBase>(); context.Setup(ctx => ctx.Request).Returns(request.Object); context.Setup(ctx => ctx.Response).Returns(response.Object); context.Setup(ctx => ctx.Session).Returns(new Mock<HttpSessionStateBase>().Object); context.Setup(ctx => ctx.Server).Returns(new Mock<HttpServerUtilityBase>().Object); context.Setup(ctx => ctx.User).Returns(GetMockMembershipUser()); context.Setup(ctx => ctx.User.Identity).Returns(context.Object.User.Identity); context.Setup(ctx => ctx.Response.Output).Returns(new StringWriter()); return context.Object; } 
+3
source share
2 answers

Hey, I think you're just having a trip here, it doesn't matter. What you described is 100% possible.

I'm not quite sure why you cannot set properties in your Mocks, but if you put the full code for your test, I would be happy to go through it with you. From head to toe, I offer two things:

  • There is a difference between installation () and SetupProperty (). Perhaps SetupProperty () is what you need if you want to keep track of property values, and not just get the value from them once.

  • Try calling SetupAllProperties () one at a time for any layout for which you want to set the property.

Take a look at the instant quick start as well as some examples.

+2
source

Not sure if anyone is interested, but I switched Moq FakeHttpContext to one using Rhino Mocks (my weapon of choice).

 public static HttpContextBase FakeHttpContext() { var httpContext = MockRepository.GenerateMock<HttpContextBase>(); var request = MockRepository.GenerateMock<HttpRequestBase>(); var response = MockRepository.GenerateMock<HttpResponseBase>(); var session = MockRepository.GenerateMock<HttpSessionStateBase>(); var server = MockRepository.GenerateMock<HttpServerUtilityBase>(); var cookies = new HttpCookieCollection(); response.Stub(r => r.Cookies).Return(cookies); request.Stub(r => r.Cookies).Return(cookies); request.Stub(r => r.Url).Return(new Uri("http://test.com")); httpContext.Stub(x => x.Server).Return(server); httpContext.Stub(x => x.Session).Return(session); httpContext.Stub(x => x.Request).Return(request); httpContext.Stub(x => x.Response).Return(response); var writer = new StringWriter(); var wr = new SimpleWorkerRequest("", "", "", "", writer); System.Web.HttpContext.Current = new System.Web.HttpContext(wr); return httpContext; } 

using in unit test

 _httpContext = FakeHttpContext(); var cookieManager = new CookieManager(_httpContext); string username = cookieManager.GetUsername(); _httpContext.AssertWasCalled(hc => { var dummy = hc.Request; }); 
+4
source

All Articles