I'm playing a bit around with the ASP.NET MVC "MIX08 preview" and I wanted to use a different approach to testing the RedirectToAction method.

SubText fellow Phil Haack wrote some sample tests back in December with the previous preview version of aspnetmvc, but I wanted to play around a bit so I wrote my own version.

public void AboutOverride()
HomeControllerForTest controller = new HomeControllerForTest();


Assert.AreEqual("Index", controller.SelectedViewName,
"Should have redirected to Index");

internal class HomeControllerForTest : HomeController
public string SelectedViewName { get; private set; }

protected override void RenderView(string viewName,
string masterName, object viewData)
SelectedViewName = viewName;

protected override void RedirectToAction(RouteValueDictionary values)
string action = (string)values["action"];
GetType().GetMethod(action).Invoke(this, null);

Here I'm using the "Extract and Override Call" testing pattern that is used in testing "Legacy code" (also called "Test Specific Subclass") to have the controller call "my" implementations of the RenderView and RedirectToAction methods that don't have dependencies on the http and  MVC contexts.

Even though it's a testing pattern for Legacy code, and the ASP.NET MVC is a brand new framework I prefer using this approach over the Catalog of TDD Anti-Pattern (I can spot at least 3 TDD anti-patterns in that test) that is the same version with mocking and setting internal behaviors, at least for this kind of tests.

Probably Phil test is more "unit" than mine since he only tests that the action only calls a redirect while mine tests 2 things: the redirect itself  and the fact that the Index action renders the Index view. Furthermore one could easily make the test pass rendering the Index view directly from the About action. But I was just playing with code, so, don't shoot me for that.

Technorati Tag: ,,