Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Unit testing ASP.NET MVC redirection

How do I Unit Test a MVC redirection?

public ActionResult Create(Product product)
{
    _productTask.Save(product);
    return RedirectToAction("Success");   
}

public ActionResult Success()
{ 
     return View();
}

Is Ayende's approach still the best way to go, with preview 5:

 public static void RenderView(this Controller self, string action) 
 {
    typeof(Controller).GetMethod("RenderView").Invoke(self,new object[] { action} ); 
 }

Seems odd to have to do this, especially as the MVC team have said they are writing the framework to be testable.

like image 486
Dan Avatar asked Sep 12 '08 08:09

Dan


1 Answers

[TestFixture]
public class RedirectTester
{
    [Test]
    public void Should_redirect_to_success_action()
    {
        var controller = new RedirectController();
        var result = controller.Index() as RedirectToRouteResult;
        Assert.That(result, Is.Not.Null);
        Assert.That(result.Values["action"], Is.EqualTo("success"));
    }
}

public class RedirectController : Controller
{
    public ActionResult Index()
    {
        return RedirectToAction("success");
    }
}
like image 77
Matt Hinze Avatar answered Oct 19 '22 02:10

Matt Hinze