11

我是 MOQ 的新手,但我将它与 NUnit 一起用于单元测试。

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

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<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;
    }

正如您在上面看到的,我试图模拟 cookie 集合的“获取”,但这没有帮助。

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

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

谢谢,

格雷格

4

3 回答 3

19

当我做 cookies.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);
于 2013-08-07T11:10:44.070 回答
1

在不使用 ActionContext 的情况下参考以前的问题和准确的解决方案:如何使用 ASP.NET 5 (MVC 6) 中的 Response 属性对控制器操作进行单元测试?[复制]

模拟 HttpResponse、HttpContext 很困难,因为它们只是只读类型。因此,要模拟这些,请在 HttpResponse 和 HttpContext 上使用 SetupGet,然后将 HttpContext 引用传递给 ControllerContext。

    var headerDict = new HeaderDictionary();
    var httpResponse = new Mock<HttpResponse>();
    var httpContext = new Mock<HttpContext>();
    
    
    httpRespnse.SetupGet(=>c.Headers).Returns(headerDict);
    httpContext.SetupGet(=>c.Response).Returns(httpResponse.Object);

    var controllerContext = new ControllerContext()
   {
       HttpContext = httpContext.Object
   };

    var controllerInstance = new ValueController()
   {
       ControllerContext = controllerContext
   };
于 2021-03-18T17:45:42.463 回答
0

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

你的嘲笑new HttpCookieCollection() 是正确的。此代码分开工作:

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();

所以错误不在那里,而是在其他地方。如果您Response.Cookies.Clear()从代码中注释掉该行会发生什么?那么其他所有东西都被正确地嘲笑了吗?当您调试测试时,您是否可以看到 mock 的其余部分是否符合预期?如果是这样,我会感到惊讶(但我之前一直很惊讶......)。

于 2013-08-07T11:03:54.313 回答