Testing Action Results with ASP.NET MVC

The latest preview of ASP.NET MVC supports returning ActionResult objects from controller actions.

This makes it very easy to test the results of an action (for example, redirecting to another controller) which was a much more involved process in previous releases.

Imagine the following controller:

public class HomeController : Controller {
	public ActionResult Index() {
		return RedirectToAction(new { controller = "Home", action = "about" });
	}

	public ActionResult About() {
		return RenderView();
	}
}

While previously you’d have to mock the Response.Redirect method, now you can do this:

var controller = new HomeController();
ActionRedirectResult result = controller.Index() as ActionRedirectResult;

if(result == null) {
	Assert.Fail("Expected an ActionRedirectResult");
}
else {
	Assert.That(result.Values["controller"], Is.EqualTo("Home"));
	Assert.That(result.Values["action"], Is.EqualTo("about"));
}

While this is certainly easier, it is still a little cumbersome. To help with this, I’ve added some extension methods to MvcContrib’s ‘TestHelper’ project. So now I can write:

var controller = new HomeController();
controller.Index().AssertIsActionRedirect().ToController("Home").ToAction("about");

Much better!

15 Responses to “Testing Action Results with ASP.NET MVC”

  1. April 28th Links: ASP.NET, ASP.NET AJAX, ASP.NET MVC, Silverlight - ScottGu's Blog Says:

    [...] Testing Action Results with ASP.NET MVC: Jeremy Skinner blogs about some cool extension method helpers he has added to MvcContrib to enable pretty sweet testing of Controller actions. [...]

  2. BusinessRx Reading List : April 28th Links: ASP.NET, ASP.NET AJAX, ASP.NET MVC, Silverlight Says:

    [...] Testing Action Results with ASP.NET MVC: Jeremy Skinner blogs about some cool extension method helpers he has added to MvcContrib to enable pretty sweet testing of Controller actions. [...]

  3. April 28th Links: ASP.NET, ASP.NET AJAX, ASP.NET MVC, Silverlight - Mirrored Blogs Says:

    [...] Testing Action Results with ASP.NET MVC: Jeremy Skinner blogs about some cool extension method helpers he has added to MvcContrib to enable pretty sweet testing of Controller actions. [...]

  4. Joshua Flanagan Says:

    Very cool. I just submitted a patch (1158) to enable:

    controller.Index().AssertIsActionRedirect().ToAction(c => c.About())

  5. Joshua Flanagan Says:

    Hmm, looks like the angle brackets got stripped from my comment.
    The ToAction method is generic and takes the type of the controller you want to redirect to. So both the controller, and the action (which is a represented by the lambda) are strongly typed.

  6. Jeremy Skinner Says:

    Nice! I’ll apply the patch when I get a moment.

  7. 4月28日链接篇: ASP.NET, ASP.NET AJAX, ASP.NET MVC, Silverlight - Joycode@Ab110.com Says:

    [...] Testing Action Results with ASP.NET MVC: Jeremy Skinner 在博客里讨论了他加到MvcContrib中的一些很酷的辅助性扩展方法,以促成对Controller action方法做称心满意的测试。 [...]

  8. April 28th Links: ASP.NET, ASP.NET AJAX, ASP.NET MVC, Silverlight « .NET Framework tips Says:

    [...] Testing Action Results with ASP.NET MVC: Jeremy Skinner blogs about some cool extension method helpers he has added to MvcContrib to enable pretty sweet testing of Controller actions. [...]

  9. ASP.NET MVC Real projects & resource pointers « Archiveloper Says:

    [...] Testing Action Results with ASP.NET MVC: Jeremy Skinner blogs about some cool extension method helpers he has added to MvcContrib to enable pretty sweet testing of Controller actions. [...]

  10. Planeta C# » April 28th Links: ASP.NET, ASP.NET AJAX, ASP.NET MVC, Silverlight Says:

    [...] Testing Action Results with ASP.NET MVC: Jeremy Skinner blogs about some cool extension method helpers he has added to MvcContrib to enable pretty sweet testing of Controller actions. [...]

  11. Silverlight Hub » April 28th Links: ASP.NET, ASP.NET AJAX, ASP.NET MVC, Silverlight Says:

    [...] Testing Action Results with ASP.NET MVC: Jeremy Skinner blogs about some cool extension method helpers he has added to MvcContrib to enable pretty sweet testing of Controller actions. [...]

  12. matias Says:

    hi!!
    How can I do with this code

    public void CalculaTest()
    {
    CalculadoraController target = new CalculadoraController(); // TODO: Initialize to an appropriate value
    string tipo = “Sumar”; // TODO: Initialize to an appropriate value
    int numero = 2; // TODO: Initialize to an appropriate value
    int numero1 = 2; // TODO: Initialize to an appropriate value

    ActionResult actual = target.Calcula(tipo, numero, numero1);

    ActionResult expected = target.Calcula(tipo, numero, numero1);

    Assert.AreEqual(expected , actual);
    //Assert.Inconclusive(“Verify the correctness of this test method.”);
    }

    I don`t know how to get a value to actual and expected because there are ActionResults.

    I’m in preview 3.

  13. Recent Links: ASP.NET, ASP.NET AJAX, ASP.NET MVC, Silverlight « Tad Wang’s Weblog Says:

    [...] Testing Action Results with ASP.NET MVC: Jeremy Skinner blogs about some cool extension method helpers he has added to MvcContrib to enable pretty sweet testing of Controller actions. [...]

  14. Jeremy Skinner Says:

    Matias,

    I don’t really understand what you’re trying to do. Perhaps a better place to ask would be on the ASP.NET MVC Forums? http://forums.asp.net/1146.aspx

  15. April 28th Links: ASP.NET, ASP.NET AJAX, ASP.NET MVC, Silverlight - Readed By Wrocław NUG members - Wroc.NET Says:

    [...] Testing Action Results with ASP.NET MVC: Jeremy Skinner blogs about some cool extension method helpers he has added to MvcContrib to enable pretty sweet testing of Controller actions. [...]


Leave a Reply