How do you unit test an HttpModule?

I would think that blasting through Google I could find an answer to this, but I'm not finding anything useful at all. How does one unit test an HttpModule? I can't imagine that it's not possible. I'm not sure how to fire off and test the various events or simulate the request/response lifecycle.

6 Comments

  • Mock Objects



    http://sourceforge.net/projects/dotnetmock

  • The first thing to do is to make the HttpModule just a thin wrapper that delegates to a normal class that does the actual work. You can unit test the normal class with some sort of mock object substituted for any dependency on the HttpContext objects.

  • Are you using NUnitAsp?

    nunitasp.sourceforge.net/

  • NUnitAsp tests too much... I want to just test the class and nothing else. Mock objects are a lot of work.

  • It was a while ago that I tried this.



    Create a web service (asmx) with your tests and mark it with the test attributes for nUnit.



    In your "real" test project, add a web reference to this service.

    Open and edit the generated proxy to add the test attributes to it.



    Configure the app that hosts the web servivce to use the http module.



    nUnit should see the Test and TestFixture in the proxy and call the right methods of the web service (I've made this work before).



    There are some issues though: single call means that SetUp and TearDown don't really work.

  • Jeff,

    Did you even find a simple way to test an HttpModule?

    I have a 50 lines module; I would prefer not having to introduce 500 lines more just to test 10 lines of logic :-).

Comments have been disabled for this content.