MOQ - Mocking MVC Controller Response.Cookies.Clear ()

I am new to MOQ, but use it with NUnit for unit testing.

I have all the parts of my controller, but only the next line that gives the error message "Object not installed in the object instance."

Response.Cookies.Clear(); 

I have the following extension method to make fun of the controller context, which works for everything else that I have come up with so far (very thanks to the nice people on this forum).

 public static int SetUpForTest(this System.Web.Mvc.Controller ctl, string username, TestingUtility.Roles role) { var routes = new RouteCollection(); MvcApplication.RegisterRoutes(routes); var request = new Mock<HttpRequestBase>(MockBehavior.Strict); request.SetupGet(x => x.ApplicationPath).Returns("/"); request.SetupGet(x => x.Url).Returns(new Uri("http://localhost/a", UriKind.Absolute)); request.SetupGet(x => x.ServerVariables).Returns(new System.Collections.Specialized.NameValueCollection()); var response = new Mock<HttpResponseBase>(MockBehavior.Strict); response.Setup(x => x.ApplyAppPathModifier(Moq.It.IsAny<String>())).Returns((String url) => url); // response.SetupGet(x => x.Cookies).Returns(new HttpCookieCollection()); // This also failed to work var context = new Mock<HttpContextBase>(MockBehavior.Strict); context.SetupGet(x => x.Request).Returns(request.Object); context.SetupGet(x => x.Response).Returns(response.Object); context.SetupGet(x => x.Response.Cookies).Returns(new HttpCookieCollection()); // still can't call the Clear() method // // Mock the controller context (using the objects mocked above) // var moqCtx = new Mock<ControllerContext>(context.Object, new RouteData(), ctl); moqCtx.SetupGet(p => p.HttpContext.User.Identity.Name).Returns(username); moqCtx.SetupGet(p => p.HttpContext.User.Identity.IsAuthenticated).Returns(true); if (!string.IsNullOrEmpty(role.ToString())) moqCtx.Setup(p => p.HttpContext.User.IsInRole(role.ToString())).Returns(true); // // Pass the mocked ControllerContext and create UrlHelper for the controller and return // ctl.ControllerContext = moqCtx.Object; ctl.Url = new UrlHelper(new RequestContext(context.Object, new RouteData()), routes); return 1; } 

As you can see above, I tried to make fun of โ€œgettingโ€ a collection of cookies, which doesn't help.

In addition, the actual Clear () method cannot be ridiculed because it is not a virtual method.

Obviously, I donโ€™t want to check that cookies are being cleared, I just want to be able to ignore it when testing.

Thanks,

Greg

+7
c # asp.net-mvc nunit moq
source share
2 answers

This works for me when I make cookie.Clear ()

  var request = new Mock<HttpRequestBase>(MockBehavior.Strict); request.SetupGet(x => x.ApplicationPath).Returns("/"); request.SetupGet(x => x.Url).Returns(new Uri("http://localhost/a", UriKind.Absolute)); request.SetupGet(x => x.ServerVariables).Returns(new System.Collections.Specialized.NameValueCollection()); var response = new Mock<HttpResponseBase>(MockBehavior.Strict); response.Setup(x => x.ApplyAppPathModifier(Moq.It.IsAny<String>())).Returns((String url) => url); // response.SetupGet(x => x.Cookies).Returns(new HttpCookieCollection()); // This also failed to work var context = new Mock<HttpContextBase>(MockBehavior.Strict); context.SetupGet(x => x.Request).Returns(request.Object); context.SetupGet(x => x.Response).Returns(response.Object); context.SetupGet(x => x.Response.Cookies).Returns(new HttpCookieCollection()); // still can't call the Clear() method context.SetupGet(p => p.User.Identity.Name).Returns("blah"); context.SetupGet(p => p.User.Identity.IsAuthenticated).Returns(true); var rc = new RequestContext(context.Object, new RouteData()); controller.ControllerContext = new ControllerContext(rc, controller); 
+14
source share

(This is only half the answer, but too large for the comment field ...)

Your mockery of new HttpCookieCollection() is correct . This code works when splitting:

 var request = new Mock<HttpRequestBase>(MockBehavior.Strict); request.SetupGet(x => x.ApplicationPath).Returns("/"); request.SetupGet(x => x.Url).Returns(new Uri("http://localhost/a", UriKind.Absolute)); request.SetupGet(x => x.ServerVariables).Returns(new System.Collections.Specialized.NameValueCollection()); var response = new Mock<HttpResponseBase>(MockBehavior.Strict); response.Setup(x => x.ApplyAppPathModifier(Moq.It.IsAny<String>())).Returns((String url) => url); // response.SetupGet(x => x.Cookies).Returns(new HttpCookieCollection()); // This also failed to work var context = new Mock<HttpContextBase>(MockBehavior.Strict); context.SetupGet(x => x.Request).Returns(request.Object); context.SetupGet(x => x.Response).Returns(response.Object); context.SetupGet(x => x.Response.Cookies).Returns(new HttpCookieCollection()); // still can't call the Clear() method // Here clearing the cookies works just fine: var instance = context.Object; instance.Response.Cookies.Clear(); 

So there is no error, but somewhere else. What happens if you comment out a line with Response.Cookies.Clear() from your code? So everything else is mocking right? When you debug the test, can you see that the rest of the layout is as expected? I would be surprised if it were (but I was surprised before ...).

0
source share

All Articles