简体   繁体   中英

Best practice for unit testing IHttpModule

I am writing an HttpModule in C#, and have written a number of unit tests to test the code.

The module is pretty simple, all it does is that it inspects a number of cookies, and expires the ones that need to be expired based on our business logic.

But I want to make sure that this does not break anything. So I want to test it good and thorough!

I have read the articles on using System.Web.Abstractions for mocking the request and response objects. This is great, and works! For more info look at: http://www.kongsli.net/nblog/2009/05/03/aspnet-35-improving-testability-with-systemwebabstractions/

But I would like to know if there is something I really need to test when it comes to writing HttpModules. Something that isn't directly related to my business logic code. Something to test that I dispose the Module correctly for example, or something else that could potentially break.

Thanks, JP

Easy:

  1. Don't put the logic inside the IHttpModule implementation.
  2. Since you only modify the cookies, just pass them to your own class.

Example:

void InYourHttpModule()
{
    myLogic.Invoke(Request.Cookies, Response.Cookies);
}

In that way it's very easy to validate that your logic do what it says and nothing else.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM