HttpContext最完整的mock框架是什么
我正在为我的应用程序中的 ASP.NET HttpContext
寻找尽可能全面的模拟替换和包装器。全面的模拟替换可能会大大提高 ASP.NET Web 应用程序的可测试性,而无需将每个应用程序迁移到更易于测试的框架(例如 MVC)。
我最感兴趣的 HttpContext
包装器和模拟框架的一些功能包括:
- 序列化会话存储(例如,
.Session
)。 - 序列化应用程序范围的存储(例如,
.Application
)。 - 每个请求的项目存储(例如,
.Items
)。 - HttpRequest 数据,例如引用者、请求 Uri、服务器变量、发布数据等。
- HttpResponse 数据,例如状态代码和内容。
- 本地文件解析(例如
Server.MapPath
) - 用于应用程序相对URL 路径解析的VirtualPathUtility,它依赖于ASP.NET 运行时。
- 用于验证身份验证/授权规则的身份和主体(例如,
.User
)。 - 用于测试
HttpModule
和Global.asax
中的错误解析的AllErrors
集合。
我考虑编写自己的接口、包装器和模拟;然而,我相信这样的东西一定已经存在了。对于初学者来说,各种各样的模拟框架有点难以理解。
您推荐的最全面的 HttpContext 包装器和模拟是什么?
I'm looking for as comprehensive as possible of a mock replacement and wrapper for the ASP.NET HttpContext
in my applications. A comprehensive mock replacement could potentially increase the testability of my ASP.NET web applications substantially, without necessitating migrating every application to more-testable frameworks such as MVC.
Some of the features I am most interested in seeing in an HttpContext
wrapper and mock framework include:
- Serialized session storage (e.g.,
.Session
). - Serialized application-scoped storage (e.g.,
.Application
). - Per-request item storage (e.g.,
.Items
). - HttpRequest data, such as referrers, request Uri, server variables, post data, etc.
- HttpResponse data, such as status codes and content.
- Local file resolution (e.g.
Server.MapPath
) - VirtualPathUtility for application-relative URL path resolution, which has a dependency on the ASP.NET runtime.
- The identity and principal (e.g.,
.User
) for validating authentication/authorization rules. - The
AllErrors
collection for testing error resolution inHttpModule
s andGlobal.asax
.
I considered writing my own interface, wrapper, and mock; however, I believe such must already exist. The variety of mock frameworks is a little overwhelming for a first-timer to absorb.
What is the most comprehensive HttpContext
wrapper and mock that you would recommend?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我的公司在为所有 http 对象(IHttpRequest、IHttpResponse 等)创建接口方面做得很好。
有点重复,但基本上需要接口上的所有方法和属性
然后为每个对象创建一个具体类型,该类型将真实类型作为构造函数参数,并将所有方法和属性传递给真实对象。
然后你可以使用 RhinoMocks 或其他工具轻松测试一切。
My company have done well with just creating interfaces for all the http objcets (IHttpRequest, IHttpResponse, etc).
It's a bit repetative but basically need all methods and properties on the interface
then create a concrete type for each which takes the real type as a constructor parameter and passes all methods and properties through to the real object.
Then you can test everything with ease using RhinoMocks or whatever..
查看 moq 框架。这是 MVC 团队使用的模拟框架,许多人(包括我自己)认为它具有最低的进入门槛。另请查看 MvcContrib 项目中的模拟助手。
Check out the moq framework. This is the mocking framework that the MVC team uses and it is considered by many (including myself) to have the lowest barrier to entry. Also check out the mocking helpers in the MvcContrib project.