Use System.Web.HttpRuntime.Cache Instead of System.Web.Caching.Cache for Unit Testing

by jmorris 24. August 2009 21:41

I ran into an interesting problem today while unit testing some code that depended upon System.Web.Cache where the Cache object itself was instantiated, but the internal dictionary was not. What happens is you end up getting null reference exceptions when you try add, remove, etc. anything from the Cache while unit testing. For example:

 



Kinda dififcult to spot what the issue is since it looks like the error is caused by the "PageTemplateKey" not matching on a cached item, but that's not the behavior of System.Web.Caching.Cache. When an item does not exist, null is returned...so what else could be causing this? The "'ContentFactory.Cache.Count' threw and exception" is the dead give away. The internal cache object was not being created in a non server environment.

For my unit tests I am using the Moq framework and mocking the major players of the ASP.NET HTTP stack. Notice that when I perform the setup on the mockContext object below I am setting the return value to be a new Cache(), this is where I am running into the issues described above.



So what to do now? There are at the least to ways to work around this issue: 1) create a mock cache wrapper or 2) use System.Web.HttpRuntime.Cache as a replacement. Opting for simplicity, I switched out the code on my MakeMockContext method to use System.Web.HttpRuntime.Cache and viola, problem solved:


This worked like a charm:

 



The reason why this works is that System.Web.Cache uses CacheInternal, which you guessed it, is set by an internal setter, called SetInternalCache. HttpRuntime directly makes the call to create CacheInternal when you access the HttpRuntime.Cache property.

Tags: , , , ,

Comments (6) -

Jay
Jay
7/14/2010 8:02:57 PM #

I went with the quick-and-dirty HttpRuntime.Cache method and it worked great.  Thanks for the post.  You saved me from driving a pen through one of my eye sockets.

Reply

cheap web hosting
cheap web hosting
12/10/2011 12:15:06 PM #

Thanks  for the Great share.
Great blog you have here.

Reply

M Nicklaus
M Nicklaus
12/21/2011 12:06:41 PM #

Hey , I really hope the housing market is a lot more desirable in 2012. I really hope you as well as the family enjoy a safe and Happy Holiday and prosperous New Year and I would expect 2012 to be a great year for all of us.

Reply

Barry
Barry
12/24/2011 2:05:25 AM #

I\\\'m so happy to read this. This is the type of manual that needs to be given and not the accidental misinformation that\\\'s at the other blogs. Appreciate your sharing this best doc.

Reply

Dinis Cruz
Dinis Cruz
1/8/2012 12:04:34 AM #

Yap, the  System.Web.HttpRuntime.Cache worked great

I'm needed this while writing UnitTest for a variation of the Javascript caching script described here: atashbahar.com/.../...oad-ASPNET-pages-faster.aspx

Reply

Rob
Rob
1/15/2012 12:00:34 AM #

It constantly amazes me how much Social Media websites have an impact on the amount of traffic that you can get when done correctly

Keep up the great work,

Rob

Reply

Add comment

  Country flag

biuquote
  • Comment
  • Preview
Loading

Who Am I?

My name is Jeff Morris a software engineer/developer in Southern California, USA. My platform du jour is dotnet, but I dabble on the darkside occasionally.

Tag cloud

Month List

Page List