Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the most complete mocking framework for 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 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?

like image 601
kbrimington Avatar asked Aug 25 '10 21:08

kbrimington


People also ask

What are mocking frameworks?

What is a mocking framework? Mocking frameworks are used to generate replacement objects like Stubs and Mocks. Mocking frameworks complement unit testing frameworks by isolating dependencies but are not substitutes for unit testing frameworks.

Which framework is used to create mocked objects?

We can use Mockito class mock() method to create a mock object of a given class or interface. This is the simplest way to mock an object. We are using JUnit 5 to write test cases in conjunction with Mockito to mock objects.

How is a mocking framework used for verification?

Mocking is a process that allows you to create a mock object that can be used to simulate the behavior of a real object. You can use the mock object to verify that the real object was called with the expected parameters, and to verify that the real object was not called with unexpected parameters.


1 Answers

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

like image 172
Michael Baldry Avatar answered Sep 21 '22 05:09

Michael Baldry