Unit test RedirectToRouteResult

4.9k views Asked by At

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?

1

There are 1 answers

0
Andreas On

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.