Issue while testing route entries using mvccontrib test helper in MVC4
-
03-07-2021 - |
Question
I am trying to write test methods for Route entries and in my route config there were a lot of entries for different action. I am using MvcContrib.TestHelper
for Testing . I am new to MVC as well as TDD. Kindly help me to solve this issue. My test case is failing and what can I do to make it pass .
Route Entry
routes.MapRoute(
name: "X1Details",
url: "X1/{X1ID}",
defaults: new { controller = "X1", action = "Index", X1ID = "X1ID" }
);
X1Controller
public ActionResult Index(int? instanceID = 0, string InfoMsg ="")
{
}
Test Method
[Test Method]
public void Should_Route_X1Index()
{
"~/X1/"
.ShouldMapTo<X1Controller>(action => action.Index(null, ""));
}
Error
Test method XXX.Tests.MVCContibRouteMapTest.Should_Route_X1IndexOf threw exception: MvcContrib.TestHelper.AssertionException: Value for parameter 'InfoMsg' did not match: expected '' but was ''; no value found in the route context action parameter named 'InfoMsg' - does your matching route contain a token called 'InfoMsg'?
Thanks in advance.
Solution
Default values don't work well with MVCContrib helper. You could modify your controller action like so:
public ActionResult Index(int? instanceID, string InfoMsg)
{
...
}
and then:
"~/X1".ShouldMapTo<X1Controller>(action => action.Index(null, null));
OTHER TIPS
I've found this approach to work, which should allow your controllers to remain the same
[Test Method]
public void Should_Route_X1Index()
{
var routeData = "~/X1/".Route();
routeData.Values["InfoMsg "] = "";
routeData.Values["instanceID"] = "0";
routeData.ShouldMapTo<X1Controller>(action => action.Index(0, ""));
}