在单元测试中使用httpcontext

发布于 2024-09-09 08:46:21 字数 377 浏览 11 评论 0原文

我正在使用 C#4.0,我需要对服务进行单元测试。服务内的函数返回类似于我调用的预期变量的路径,这是我期望返回的路径。但是当我运行此测试时,我收到 HttpContext.Current 为 NULL 的错误。我可以做什么来解决这个问题以便可以运行测试?

[TestMethod]
public void GetPathTest()
{
   var expected = System.IO.Path.GetFullPath(HttpContext.Current.Server.MapPath("~/Certificates/"));
   var path = _mockService.Setup(o => o.GetPath()).Returns(expected);
}

I'm using C#4.0 and i need to unit test a service. The function inside the service returns a path similar to the variable i called expected, this is the path i'm expecting to get back. But when i run this test i'm getting the error that HttpContext.Current is NULL. What can i do to fix this issue so the test can be ran?

[TestMethod]
public void GetPathTest()
{
   var expected = System.IO.Path.GetFullPath(HttpContext.Current.Server.MapPath("~/Certificates/"));
   var path = _mockService.Setup(o => o.GetPath()).Returns(expected);
}

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

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

发布评论

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

评论(4

夏花。依旧 2024-09-16 08:46:21

目前,我找不到之前使用的 HttpContext 的完整包装器,但目前我们只需为空请求创建一个上下文,然后从那里开始,如下所示:

SimpleWorkerRequest request = new SimpleWorkerRequest("","","", null, new StringWriter());
HttpContext context = new HttpContext(request);

然后在单元测试中初始化或在单元测试中本身(在创建预期之前),您可以按如下方式设置当前的 HttpContext:

HttpContext.Current = context;

然后根据需要简单地充实假上下文和可能的假会话状态等。

(编辑:这都是在VS2008中,顺便说一下,框架3.5)。

At the moment I can't find my full wrapper for HttpContext that I used earlier, but at the moment we simply create a context for an empty request and go from there, like this:

SimpleWorkerRequest request = new SimpleWorkerRequest("","","", null, new StringWriter());
HttpContext context = new HttpContext(request);

Then in the unit test initialize or in the unit test itself (before you create expected) you can set the current HttpContext as follows:

HttpContext.Current = context;

Then simply flesh out the fake context and possible fake sessionstate, etc as required.

(Edit: This is all in VS2008, framework 3.5 by the way).

油焖大侠 2024-09-16 08:46:21

您可以尝试查看为 ASP.Net 单元测试创​​建的属性,例如

[HostType("ASP.NET")]

This MSDN 链接 有一篇关于它的很好的文章

You can try looking at the attributes created for ASP.Net unit testing, like

[HostType("ASP.NET")]

This link to MSDN has quite a good write-up about it

帅的被狗咬 2024-09-16 08:46:21

您可以使用以下属性来装饰您的测试方法:

[TestMethod]
[HostType("ASP.NET")]
[UrlToTest("http://localhost:xxxx/")]
[AspNetDevelopmentServerHost("$(SolutionDir)\\xxx\\", "/")]
public void TestMethod()
{
   ...
}

然后将 Default.aspx 文件添加到您的单元测试项目中。

在测试方法中,您可以轻松访问 HttpContext。
如果你想调试,你可以使用一些跟踪或使用指令 System.Diagnostics.Debugger.Break() 中断调试执行,

public void TestMethod()
{
   System.Diagnostics.Debugger.Break();

   ...
}

然后将调试器附加到进程,如 MSDN 所解释的:
https://msdn.microsoft.com/ en-us/library/vstudio/c6wf8e4z(v=vs.100).aspx

You could decorate your test method with the followings attributes:

[TestMethod]
[HostType("ASP.NET")]
[UrlToTest("http://localhost:xxxx/")]
[AspNetDevelopmentServerHost("$(SolutionDir)\\xxx\\", "/")]
public void TestMethod()
{
   ...
}

Then adding a Default.aspx file into your unit test proj.

Inside the test method you can easily access to the HttpContext.
If you want to debug, you may use some traces or interrupt the debugging execution with the instruction System.Diagnostics.Debugger.Break()

public void TestMethod()
{
   System.Diagnostics.Debugger.Break();

   ...
}

and then attaching debugger to the process as explained by MSDN:
https://msdn.microsoft.com/en-us/library/vstudio/c6wf8e4z(v=vs.100).aspx

梦明 2024-09-16 08:46:21

我把这个贴出来供参考。这不是一个简单的解决方案,并讨论鸭子打字(如果它嘎嘎叫..):

http:// /haacked.com/archive/2007/08/19/why-duck-typing-matters-to-c-developers.aspx

http://haacked.com/archive/2007/09/09/ihttpcontext-and-other-interfaces-for-your-duck-typing-benefit.aspx

相关且值得一读;因为没有 IHttpContext,所以不可能创建测试环境实现 - 直到您考虑在此处使用 Duck Typing 库。虽然这不是一个直接的答案。

希望有帮助。

I'm posting this for reference. It's not an easy solution, and talks about Duck Typing (if it quacks..):

http://haacked.com/archive/2007/08/19/why-duck-typing-matters-to-c-developers.aspx

http://haacked.com/archive/2007/09/09/ihttpcontext-and-other-interfaces-for-your-duck-typing-benefit.aspx

It's relevant and worth a read; because there's no IHttpContext it's not possible to create a test environment implementation - until you consider using the Duck Typing library here. Though this is not a direct answer.

Hope that helps.

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