TestControllerBuilder in MvcContrib
Yesterday, I cracked open the MvcContrib source code to see if it had....ummm...ok, I swear I was looking for something legitimate in there. But as soon as I updated the latest and opened the project, my eyes zeroed in on the TestControllerHelper class. So with a quick mutter of "oooh, shiny" to myself, I proceeded to what I knew would be the dearth of my productivity for the rest of my day.
Sitting here this morning, I can report to you that I am now one step closer to being able to adequately (but still not quite usefully) test controller actions that include a RedirectToAction method. Here's a sample test:
[ Test ] public void Test_redirect( ) { TestControllerBuilder builder = new TestControllerBuilder( ); var controller = builder.CreateController<LocationController>( ); controller.TestRedirect( ); Assert.That( builder.RedirectToActionData.ControllerName, Is.EqualTo( "Job" ) ); }
The CreateController method on my TestControllerBuilder uses Castle's DynamicProxy to create the controller. It also creates a ControllerContext using dynamic mocks from Rhino Mocks similar to the way Haackselman have already showed you and wires it up to the controller.
An interceptor is also attached to the controller to intercept calls to RenderView and RedirectToAction. (Actually, it intercepts all calls but only these two have special handling.) Calls to either method will populate an appropriate object on the builder class. In the example above, I'm using the RedirectToActionData object.
So far, this is all sweet and dandy like raccoon candy. I have indeed verified that it works as advertised. But at present, it seems to work only for controllers that have a parameterless constructor. Which is going to work for exactly none of my controllers.
I have a version working that allows you to pass in constructor arguments to CreateController and will submit it as a patch for MvcContrib, assuming it hasn't already been addressed.
Oh yeah, I remember! I wanted to see if MvcContrib had the Flash feature I saw added to (but not yet used in) CodeCampServer.
Kyle the Forget-Me-Not