First time here? You are looking at the most recent posts. You may also want to check out older archives or the tag cloud. Please leave a comment, ask a question and consider subscribing to the latest posts via RSS. Thank you for visiting! (hide this)

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.

[TestMethod]
public void AboutOverride()
{
HomeControllerForTest controller = new HomeControllerForTest();

controller.About();

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: ,,
posted on Monday, March 10, 2008 4:51 PM
Comments have been closed on this topic.