What is the most complete mocking framework for HttpContext
Asked Answered
D

2

9

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 in HttpModules and Global.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?

Dorisdorisa answered 25/8, 2010 at 21:50 Comment(0)
C
2

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..

Confiteor answered 27/8, 2010 at 8:18 Comment(1)
It would appear that you are the most correct. .NET 3.5 introduces the HttpContextBase, but prior to that, mocking was only possible with custom wrappers. I think it would be beneficial to see a well-designed mock wrapper for HttpContext to facilitate improved automated functional testing. I'll keep my eyes out.Dorisdorisa
B
1

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.

Benzaldehyde answered 26/8, 2010 at 21:24 Comment(3)
The question is not for ASP.NET MVC, but for ASP.NET Web Forms. For MVC, mocking the HTTP context is easy. :)Morganite
No need for down-votes here. I'll take a look at moq. Is it possible/practical to use MvcContrib classes in traditional ASP.NET?Dorisdorisa
@Dorisdorisa No, MvcContrib is designed to add functionality and ease-of-use to Microsoft's ASP.NET MVC Framework and not usable for Web Forms. But it would be very interesting if you got moq to play with Web Forms. :)Morganite

© 2022 - 2024 — McMap. All rights reserved.