<p>How do I Unit Test a MVC redirection?</p>

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

public ActionResult Success()
{ 
     return View();
}
</code></pre>

<p>Is <a href="http://www.ayende.com/Blog/archive/2007/12/13/Dont-like-visibility-levels-change-that.aspx" rel="nofollow noreferrer">Ayende's</a> approach still the best way to go, with preview 5:</p>

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

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

[{'answer_id': 58789, 'author': 'Community', 'author_id': -1, 'author_profile': 'https://Stackoverflow.com/users/-1', 'pm_score': -1, 'selected': False, 'text': '<p>You can assert on the ActionResult that is returned, you\'ll need to cast it to the appropriate type but it does allow you to use state-based testing. A search on the Web should find some useful links, here\'s <a href="http://sebastienlachance.com/2008/05/06/testing-controllers-in-aspnet-mvc-aka-actionresult/" rel="nofollow noreferrer">just one</a> though.</p>\n'}, {'answer_id': 58818, 'author': 'Matt Hinze', 'author_id': 2676, 'author_profile': 'https://Stackoverflow.com/users/2676', 'pm_score': 6, 'selected': True, 'text': '<pre><code>[TestFixture]\npublic class RedirectTester\n{\n    [Test]\n    public void Should_redirect_to_success_action()\n    {\n        var controller = new RedirectController();\n        var result = controller.Index() as RedirectToRouteResult;\n        Assert.That(result, Is.Not.Null);\n        Assert.That(result.Values["action"], Is.EqualTo("success"));\n    }\n}\n\npublic class RedirectController : Controller\n{\n    public ActionResult Index()\n    {\n        return RedirectToAction("success");\n    }\n}\n</code></pre>\n'}, {'answer_id': 3057516, 'author': 'hurbata', 'author_id': 368725, 'author_profile': 'https://Stackoverflow.com/users/368725', 'pm_score': -1, 'selected': False, 'text': '<p>you can use Mvc.Contrib.TestHelper which provides assertions for testing redirections. Take a look at <a href="http://kbochevski.blogspot.com/2010/06/unit-testing-mvcnet.html" rel="nofollow noreferrer">http://kbochevski.blogspot.com/2010/06/unit-testing-mvcnet.html</a> and the code sample. It might be helpful. </p>\n'}, {'answer_id': 35017463, 'author': "Kevin O'Shaughnessy", 'author_id': 4253016, 'author_profile': 'https://Stackoverflow.com/users/4253016', 'pm_score': 0, 'selected': False, 'text': '<p>This works for ASP.NET MVC 5 using NUnit:</p>\n\n<pre><code>    [Test]\n    public void ShouldRedirectToSuccessAction()\n    {\n        var controller = new RedirectController();\n        var result = controller.Index() as RedirectToRouteResult;\n\n        Assert.That(result.RouteValues["action"], Is.EqualTo("success"));\n    }\n</code></pre>\n\n<p>If you want to test that you are redirecting to a different controller (say NewController), the assertion would be:</p>\n\n<pre><code>Assert.That(result.RouteValues["controller"], Is.EqualTo("New"));\n</code></pre>\n'}]
['https://Stackoverflow.com/questions/58513', 'https://Stackoverflow.com', 'https://Stackoverflow.com/users/230/']