ASP.Net MVC 单元测试异常

发布于 2024-08-21 02:47:44 字数 2029 浏览 4 评论 0原文

我正在使用 Moq 框架进行单元测试。我遵循此处列出的一些非常有用的说明来帮助我模拟 httpcontext,特别是为了测试 url 引用:

http://my6solutions.com/post/2009/08/18/Mocking-HttpContext-in-ASP-NET-MVC.aspx

一切似乎都编译得很好,但是在运行测试时出现以下错误:

System.Web.HttpException:无效使用 SimpleWorkerRequest 构造函数。 应用程序路径不能被覆盖 在此背景下。请使用 SimpleWorkerRequest 构造函数 不覆盖应用程序 路径..

时,我发现错误似乎抛出在以下行:

var wr = new SimpleWorkerRequest("", "", "", "", writer);

我不知道该怎么做。什么是 SimpleWorkerRequest 以及它在 Moq 中创建 HttpContext 时如何工作?为什么根据调试器我对它的使用无效?

更新:这是我使用的完整方法,取自上面的网站,

public static HttpContextBase FakeHttpContext()
        {
            var httpContext = new Mock<HttpContextBase>();
            var request = new Mock<HttpRequestBase>();
            var response = new Mock<HttpResponseBase>();
            var session = new Mock<HttpSessionStateBase>();
            var server = new Mock<HttpServerUtilityBase>();
            var cookies = new HttpCookieCollection();

            httpContext.Setup(x => x.Server).Returns(server.Object);
            httpContext.Setup(x => x.Session).Returns(session.Object);
            httpContext.Setup(x => x.Request).Returns(request.Object);
            httpContext.Setup(x => x.Response).Returns(response.Object);

            response.Setup(x => x.Cookies).Returns(cookies);
            httpContext.SetupGet(x => x.Request.Url).Returns(new Uri("http://www.csuci.edu"));
            httpContext.SetupGet(x => x.Request.UrlReferrer).Returns(new Uri("http://www.csuci.edu"));
            //var writer = new StringWriter();
            //var wr = new SimpleWorkerRequest("", "", "", "", writer);
            //HttpContext.Current = new HttpContext(wr);
            return httpContext.Object;
        }

我注释掉了问题行,似乎解决了它,但我仍然不确定这些行应该做什么以及为什么它们会导致错误。

I'm using the Moq framework to do my unit testing. I'm following some really useful instructions outlined here to help me mock the httpcontext, specifically for the purposes of testing the url referrer:

http://my6solutions.com/post/2009/08/18/Mocking-HttpContext-in-ASP-NET-MVC.aspx

Everything seems to compile fine, however when running the test I get the following error:

System.Web.HttpException: Invalid use
of SimpleWorkerRequest constructor.
Application path cannot be overridden
in this context. Please use
SimpleWorkerRequest constructor that
does not override the application
path..

When analyzing I find the error seems to be thrown at the following line:

var wr = new SimpleWorkerRequest("", "", "", "", writer);

I'm not sure what to make of this. What is the SimpleWorkerRequest and how does it work in creating the HttpContext in Moq?? Why is my use of it invalid according to the debugger??

UPDATE: Here is the complete method I use taken from the website above

public static HttpContextBase FakeHttpContext()
        {
            var httpContext = new Mock<HttpContextBase>();
            var request = new Mock<HttpRequestBase>();
            var response = new Mock<HttpResponseBase>();
            var session = new Mock<HttpSessionStateBase>();
            var server = new Mock<HttpServerUtilityBase>();
            var cookies = new HttpCookieCollection();

            httpContext.Setup(x => x.Server).Returns(server.Object);
            httpContext.Setup(x => x.Session).Returns(session.Object);
            httpContext.Setup(x => x.Request).Returns(request.Object);
            httpContext.Setup(x => x.Response).Returns(response.Object);

            response.Setup(x => x.Cookies).Returns(cookies);
            httpContext.SetupGet(x => x.Request.Url).Returns(new Uri("http://www.csuci.edu"));
            httpContext.SetupGet(x => x.Request.UrlReferrer).Returns(new Uri("http://www.csuci.edu"));
            //var writer = new StringWriter();
            //var wr = new SimpleWorkerRequest("", "", "", "", writer);
            //HttpContext.Current = new HttpContext(wr);
            return httpContext.Object;
        }

I commented out the problem lines, that seemed to fix it, but I'm still not sure what those lines were supposed to do and why they were causing error.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

沧笙踏歌 2024-08-28 02:47:44

请参阅上面的更新部分,如果您按照上面链接中提供的示例操作并删除上面注释掉的特定行,如果您要寻找的只是伪造 UrlReferrer(这就是我所追求的),那么这似乎可以解决问题。不确定这将如何影响假 httpContext 的其余部分,因为我不确定这些行的确切用途是什么。

See UPDATE section above, if you follow the example provided in the link above and remove the specific lines commented out above, that seems to fix the problem if all you're looking for is to fake the UrlReferrer, which is what I was after. Not sure how that would affect the rest of the fake httpContext, as I'm not sure exactly what the purpose of those lines was.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文