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?
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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With