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?
There is a more efficent way since you don't need to test these two lines
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.