Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Unit test RedirectToRouteResult

I have the following code in my controller:

public class MyController : BaseController
{
    public ActionResult MyMethod()
    {
        ...
        return RedirectToAction("Index", "Dashboard");
    }
}

I'd like to unit test this redirect (RedirectToRouteResult). I've done it this way:

Assert.IsTrue(result.RouteValues.ContainsKey("action"));
Assert.IsTrue(result.RouteValues.ContainsKey("controller"));
Assert.AreEqual("Index", result.RouteValues["action"].ToString());
Assert.AreEqual("Dashboard", result.RouteValues["controller"].ToString());

So I need four asserts to test my RedirectToRouteResult. Is there any more efficient way?

like image 280
mosquito87 Avatar asked Jul 26 '13 08:07

mosquito87


People also ask

What is unit testing in MVC?

In computer programming, unit testing is a software testing method by which individual units of source code are tested to determine whether they are fit for use.


1 Answers

There is a more efficent way since you don't need to test these two lines

Assert.IsTrue(result.RouteValues.ContainsKey("action"));
Assert.IsTrue(result.RouteValues.ContainsKey("controller"));

Those are assertions for code that you have not written. You have to trust that those who writes that code have there own unit tests. If against all odds the first two lines would be faulty, your two final assertions would fail.

like image 139
Andreas Avatar answered Oct 02 '22 12:10

Andreas