MOQ – 模拟MVC控制器的Response.Cookies.Clear()

我是MOQ的新手,但我正在使用NUnit进行unit testing。

我已经模拟了我的控制器的所有部分,除了以下行抛出“对象未设置为对象的实例”错误消息。

Response.Cookies.Clear(); 

我有以下扩展方法来模拟控制器上下文,它适用于我迄今为止所遇到的所有其他事情(非常感谢此论坛上的好人)。

 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(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(MockBehavior.Strict); response.Setup(x => x.ApplyAppPathModifier(Moq.It.IsAny())).Returns((String url) => url); // response.SetupGet(x => x.Cookies).Returns(new HttpCookieCollection()); // This also failed to work var context = new Mock(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(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; } 

正如您在上面所看到的,我试图模仿cookie集合的“获取”,这没有用。

此外,实际的Clear()方法不能被模拟,因为它不是虚方法。

显然我不想测试cookie被清除,我只是想在测试中忽略它。

谢谢,

格雷格

当我做cookies时,这对我有用。清除()

  var request = new Mock(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(MockBehavior.Strict); response.Setup(x => x.ApplyAppPathModifier(Moq.It.IsAny())).Returns((String url) => url); // response.SetupGet(x => x.Cookies).Returns(new HttpCookieCollection()); // This also failed to work var context = new Mock(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); 

(这只是答案的一半,但对于评论字段来说太大了……)

你嘲笑new HttpCookieCollection() 是正确的 。 此代码适用于分离:

 var request = new Mock(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(MockBehavior.Strict); response.Setup(x => x.ApplyAppPathModifier(Moq.It.IsAny())).Returns((String url) => url); // response.SetupGet(x => x.Cookies).Returns(new HttpCookieCollection()); // This also failed to work var context = new Mock(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(); 

所以错误不在那里,而在其他地方。 如果您从代码中注释掉Response.Cookies.Clear()的行,会发生什么? 其他一切都被嘲弄了吗? 调试测试时,您是否可以看到模拟的其余部分符合预期? 如果是的话我会感到惊讶(但我之前感到很惊讶……)。