Testing Action Results with ASP.NET MVC
April 19, 2008 – 6:50 pm
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!
16 Responses
[...] 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. [...]
[...] 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. [...]
[...] 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. [...]
Nice! I’ll apply the patch when I get a moment.
Very cool. I just submitted a patch (1158) to enable:
controller.Index().AssertIsActionRedirect().ToAction(c => c.About())
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.
[...] Testing Action Results with ASP.NET MVC: Jeremy Skinner 在博客里讨论了他加到MvcContrib中的一些很酷的辅助性扩展方法,以促成对Controller action方法做称心满意的测试。 [...]
[...] 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. [...]
[...] 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. [...]
[...] 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. [...]
[...] 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. [...]
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.
[...] 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. [...]
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
[...] 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. [...]
Great blog post! I am having a blast playing with ASP.NET MVC. Thanks.