对于HttpContext的最佳实践,并在ASP.Net MVC可测试controlers [英] Best practices for HttpContext and testable controlers in ASP.Net MVC

查看:275
本文介绍了对于HttpContext的最佳实践,并在ASP.Net MVC可测试controlers的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

更新:

根据一对夫妇我收到的答案,我只是想清楚,我清楚地知道如何去用讽刺的框架嘲讽的HttpContext。 我更感兴趣知道什么时候相比,使用围绕HttpContext的包装类嘲讽的HttpContext的利弊。

Based on a couple of the answers I have received, I just want to make clear that I am well aware how to go about mocking HttpContext using a mocking framework. I am more interested knowing what the pros and cons of mocking HttpContext are when compared to using wrapper classes around HttpContext.


我在寻找如何在ASP.Net MVC构建可测试的控制器时,处理HttpContext的意见。它看完后似乎有两个思想流派 - 无论是蓄客HttpContextBase的,并使用一个嘲讽的框架来生成所需的存根/嘲笑你的单元测试或围绕你打算使用HttpContext的领域无关的包装类

I'm looking for opinions on how to deal with HttpContext when building testable controllers in ASP.Net MVC. After reading up on it there seems to be two schools of thought - either build off of HttpContextBase and use a mocking framework to generate the needed stubs/mocks for your unit testing or build agnostic wrapper classes around the areas of HttpContext which you intend to use.

现在我朝着建立了HttpContextBase的倾斜。现在看来似乎既是更快的发展过程中,更易于维护,你不必花时间开发和维护额外的包装类。我可以看到包装类可能是有益的,因为它们抽象掉底层的实现和保持控制器的情况下分开的要求 - 但我不知道这是值得的额外开销,安装和维护

Right now I am leaning towards building off of HttpContextBase. It seems like it is both the faster development process and easier to maintain as you don't have to spend time developing and maintaining additional wrapper classes. I can see how the wrapper classes might be beneficial as they abstract away the underlying implementation and keep the controller's context separate from the request - but I'm not sure if this is worth the extra overhead to setup and maintain.

你有什么感觉是这两种方法时,你会选择一个比其他的利弊?是借给自己的解决方案之一,有某些类型的发展更比其他?

What do you feel are the pros and cons between these two approaches and when would you choose one over the other? Are there certain types of development that lend itself to one of the solutions more so than the other?

由于这似乎是一个常见的​​问题,大多数球队谁是单元测试和使用ASP.Net MVC要处理,你会怎么还是你去怎样处理这个问题?如果你已经解决了这个问题,怎么做您的解决方案的工作,你会怎么做,现在不同?

As this seems like a common issue most teams who are unit testing and using ASP.Net MVC have to deal with, how would you or have you gone about handling this issue? If you have addressed this issue, how did your solution work and what would you do differently now?

谢谢!

推荐答案

我倾向于HttpContextBase。主要是因为我觉得它的发明因为这个原因:可测试性。以及为何另起炉灶当有一个可接受的解决方案已经在那里。

I'm leaning towards HttpContextBase. Mainly because I think it was invented for just this reason: testability. And why reinvent the wheel when there's an acceptable solution already out there.

如果你投入了大量的精力投入到各地的HttpContext的包装类,你会最终以HttpContextBase ...

If you put a lot of effort into your wrapper classes around HttpContext, you'd end up with something very similar to HttpContextBase...

这篇关于对于HttpContext的最佳实践,并在ASP.Net MVC可测试controlers的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆