在单元测试中使用httpcontext
我正在使用 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
目前,我找不到之前使用的 HttpContext 的完整包装器,但目前我们只需为空请求创建一个上下文,然后从那里开始,如下所示:
然后在单元测试中初始化或在单元测试中本身(在创建预期之前),您可以按如下方式设置当前的 HttpContext:
然后根据需要简单地充实假上下文和可能的假会话状态等。
(编辑:这都是在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:
Then in the unit test initialize or in the unit test itself (before you create expected) you can set the current HttpContext as follows:
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).
您可以尝试查看为 ASP.Net 单元测试创建的属性,例如
This MSDN 链接 有一篇关于它的很好的文章
You can try looking at the attributes created for ASP.Net unit testing, like
This link to MSDN has quite a good write-up about it
您可以使用以下属性来装饰您的测试方法:
然后将 Default.aspx 文件添加到您的单元测试项目中。
在测试方法中,您可以轻松访问 HttpContext。
如果你想调试,你可以使用一些跟踪或使用指令 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:
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()
and then attaching debugger to the process as explained by MSDN:
https://msdn.microsoft.com/en-us/library/vstudio/c6wf8e4z(v=vs.100).aspx
我把这个贴出来供参考。这不是一个简单的解决方案,并讨论鸭子打字(如果它嘎嘎叫..):
相关且值得一读;因为没有 IHttpContext,所以不可能创建测试环境实现 - 直到您考虑在此处使用 Duck Typing 库。虽然这不是一个直接的答案。
希望有帮助。
I'm posting this for reference. It's not an easy solution, and talks about Duck Typing (if it quacks..):
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.