Jeff and .NET

The .NET musings of Jeff Putz

Sponsors

News

My Sites

Archives

Unit testing with HttpContext

I've become a real fan of unit testing, though I was somewhat skeptical about its use in Web apps. NUnitASP is pretty cool for testing UI, but what I really needed was a way to test classes running in the context of a Web app, not actual requests for pages.

I do some caching voodoo in POP Forums to reduce database hits. Naturally this relies on the cache of the Web app, which you don't have in a unit testing context. Bummer! After some searching and link hopping I got to this article by Steve Padfield. Bam! That's what I needed. I only needed to make it work in .NET v2.

After some messing around, I arrived at this:

TextWriter tw = new StringWriter();
HttpWorkerRequest
wr = new SimpleWorkerRequest("/webapp", "c:\\inetpub\\wwwroot\\webapp\\", "default.aspx", "", tw);
HttpContext
.Current = new HttpContext(wr);

Pop that code in any test that will call a class that needs a non-null HttpContext. Works like a champ, and life is grand. Now if I can only get the damn tests, or the assembly being tested, to see my config settings.

Posted: May 31 2004, 07:42 PM by Jeff | with 13 comment(s)
Filed under: ,

Comments

steven said:

Hi there,
I'm just having a little trouble with this - trying to get the example from Steve Padfield's article working.
Have put your line in: HttpWorkerRequest wr = new SimpleWorkerRequest("/webapp", "c:\\inetpub\\wwwroot\\webapp\\", "default.aspx", "", tw);

...but get this exception:
System.Web.HttpException : Invalid use of SimpleWorkerRequest constructor. Application path cannot be overridden in this context. Please use SimpleWorkerRequest constructor that does not override the application path.

Can't find anything on google about it - - did you see this when you were trying things out?
Any ideas very gratefully received!
Thanks
S
# June 3, 2004 11:45 AM

Jeff said:

My example was on v2 of the framework, the alpha (May build). I don't think the current versions have the same overload, and as such you'd need to use Steve's code.
# June 3, 2004 7:54 PM

TrackBack said:

# June 4, 2004 11:30 PM

Karl said:

As far as I can tell, this has extremely limited capabilities. Namely, an HttpRuntime is never created, which renders all but basic unit testing useless.
# July 1, 2004 11:17 AM

Jeff said:

Useless? If I can store things in the cache, the only thing I've encountered that I need, then it's not useless at all. Just because you don't see a use for it doesn't mean other people might not.
# July 1, 2004 12:08 PM

Unit Testing with HttpContext « The Pursuit of a Life said:

Pingback from  Unit Testing with HttpContext « The Pursuit of a Life

# November 28, 2007 4:11 PM

stefan.sedich said:

Thanks this was very useful to me, needed to test my cache provider, this works nice :)

Cheers

Stefan

# August 26, 2008 10:28 AM

Phil Steel said:

You can also access the web cache via HttpRuntime.Cache. Have a look at thisd article:

weblogs.asp.net/.../437559.aspx

# October 31, 2008 12:44 PM

Jeff said:

So true... I learned that about a year later, and it really makes a huge difference.

# October 31, 2008 12:48 PM

Nathan said:

The only caveat emptorium is that if you go that route you can't go the route of using the built in .net test deployment infrastructure. Means you also have to dynamically build all the controls you are interested in which means also creating an instance of the ControlAdapter which means calling a protected RenderControl with a mock class . . .

ad infinitum?

yak shavings?

# December 4, 2008 9:23 AM

Chris said:

Thanks so much. worked a treat.

# December 2, 2009 8:07 PM

Ian said:

Worked great, but what if I want to create a context that has the IsSecureConnection property set to true?  I need to fake out a secure HTTPS connection to test a code path.

# March 2, 2010 8:14 AM

satish said:

Hi Jeff,

I am using .net 3.5 framework, your code works fine, when i want to use caching however, when i try to access currentcontext session, it is null.

Cant we use the code for accessing session?

# May 18, 2010 2:17 AM
Leave a Comment

(required) 

(required) 

(optional)

(required)