I think it would be useful if IHttpContext.Cache exposed an IHttpCache interface rather than a concrete Cache object. This would make unit testing code that interacts with the cache much easier.
I am trying to test an action that stores some data in the HttpContext.Cache. What I would like to be able to do is create a mock Cache object so that I can assert that the correct data has been stored.
My current solution is to create a wrapper object for the cache. While this works, it would be nice to see it in the framework by default, especially as other properties on httpcontext have been abstracted this way (Reques, Response, Session etc)
Ah, I see +1 to an interface. Or at least make the default constructor of Cache new up an instance of one of the internal CacheInternal implementations so a simple call to new Cache() can actually be used.
Thanks - I'll use this approach in my tests for now. It seems cleaner than the wrapper object that I was using. The only caveat is having to explicitly remove all items from the cache in my test Setup method.
JeremyS
Member
506 Points
99 Posts
Suggestion: Add an IHttpCache interface
Dec 15, 2007 03:18 PM|LINK
I think it would be useful if IHttpContext.Cache exposed an IHttpCache interface rather than a concrete Cache object. This would make unit testing code that interacts with the cache much easier.
Thanks
abombss
Member
575 Points
164 Posts
Re: Suggestion: Add an IHttpCache interface
Dec 15, 2007 07:38 PM|LINK
What are you trying to test and how do you intend to use it?
ASP.NET MVC MVC TDD Cache
JeremyS
Member
506 Points
99 Posts
Re: Suggestion: Add an IHttpCache interface
Dec 16, 2007 11:33 AM|LINK
I am trying to test an action that stores some data in the HttpContext.Cache. What I would like to be able to do is create a mock Cache object so that I can assert that the correct data has been stored.
My current solution is to create a wrapper object for the cache. While this works, it would be nice to see it in the framework by default, especially as other properties on httpcontext have been abstracted this way (Reques, Response, Session etc)
abombss
Member
575 Points
164 Posts
Re: Suggestion: Add an IHttpCache interface
Dec 16, 2007 04:55 PM|LINK
Ah, I see +1 to an interface. Or at least make the default constructor of Cache new up an instance of one of the internal CacheInternal implementations so a simple call to new Cache() can actually be used.
MrKurt
Member
52 Points
12 Posts
Re: Suggestion: Add an IHttpCache interface
Dec 16, 2007 06:27 PM|LINK
System.Web.HttpRuntime.Cache
That said, an interface for that would be nice for a lot of reasons.
JeremyS
Member
506 Points
99 Posts
Re: Suggestion: Add an IHttpCache interface
Dec 16, 2007 07:19 PM|LINK
Thanks - I'll use this approach in my tests for now. It seems cleaner than the wrapper object that I was using. The only caveat is having to explicitly remove all items from the cache in my test Setup method.
An interface would be better though ;)