Alex Rivera | Logout

How do you mock the caching object in asp.net mvc?

Asked 2009-04-23T19:39:34.027
17

How would I mock the caching object on the ControllerContext object for my unit tests? I have tried creating a wrapper class like the following (since the cache object is a sealed class) with no luck.

var mockControllerContext = new Mock<ControllerContext>(); 
var mockhttpContext = new Mock<HttpContextBase>();            

mockhttpContext.SetupGet(o => o.Cache).Returns(
         new CacheWrapper(mockControllerContext.Object.HttpContext.Cache));

mockControllerContext.SetupGet(
                          o => o.HttpContext).Returns(mockhttpContext.Object);
this.tennisMatchupController.ControllerContext = mockControllerContext.Object; 
Edit
Report

1 Answer

0

You can try to use Typemock Isolator, it fakes sealed classes out of the box, so you won't need these wrappers.

answered 2009-05-28T12:01:10.643

Your Answer