HTTPSimulator - Simulating HTTP Requests for unit testing made easier

Phill Haack just released HTTPSimulator - a class to help with running tests against a simulated Http Context, to help simulate requests and more without resorting to needing a web server running for the tests. [via DNK]

I like this approach but I also like Phil's comments at the end. Read them. He's right.

Also noted:

"originally tried to do all this by using the public APIs. Unfortunately, so many classes are internal or sealed that I had to get my hands dirty and resort to using reflection. Doing so freed me up to finally get certain features working that I could not before."

This again goes to the Testable Object Oriented Design (TOOD) idea that I wrote about. Testable design might mean two categories:

  • A Test Enableing\Disabling design (Sealed classes, high coupling etc. is preventing us from writing tests that simulate or stub out parts of the design)
  • A testable\non testable design - meaning you can easily or not easily test the parts in the code you'd like to test.

In this case the HTTP related classes are test-disablers in that we cannot easily stub out their features to test something that relies on them.

Published Wednesday, June 20, 2007 4:20 PM by RoyOsherove

Comments

Sunday, August 26, 2007 5:27 AM by Ulu

# re: HTTPSimulator - Simulating HTTP Requests for unit testing made easier

I've discovered a way to achieve better testability with Asp.Net. We have several frameworks that can provide the html output of your test page, but the problem is that these are actually integration tests (so you have to setup your dependencies instead of mocking them), and also these are low-level tests (you look for substrings in the html output instead of directly testing your control's properties).

My recipe is: Plasma + TypeMock. I use Plasma for in-process testing and TypeMock to get access to the HTTPContext and Page objects. So far, I haven't used it in any tests, but I've got a valid reference to my page and verified that its controls are there, and this is a big step.

The details are here: dotfresh.blogspot.com/.../aspnet-tesability-finally-way-it-should.html

ulu