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!

6 Comments

  1. Posted April 29, 2008 at 3:41 pm | Permalink

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

  2. Posted April 29, 2008 at 3:25 pm | Permalink

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

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

  3. Posted April 29, 2008 at 3:27 pm | Permalink

    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.

  4. matias
    Posted June 6, 2008 at 8:39 pm | Permalink

    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.

  5. Posted June 8, 2008 at 11:45 am | Permalink

    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

  6. Posted July 3, 2008 at 2:00 pm | Permalink

    Great blog post! I am having a blast playing with ASP.NET MVC. Thanks.

10 Trackbacks

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

  5. [...] 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. [...]

  6. [...] 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. [...]

  7. [...] 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. [...]

  8. [...] 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. [...] 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. [...] 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. [...]

Post a Comment

Your email is never published nor shared. Required fields are marked *

*
*