MvcContrib ShouldMapTo TestHelper unexpectedly throws an AssertionException

I get the expected error when testing a route using the MvcContrib ShouldMapTo function. According to the results, everything is fine, but the helper throws an AssertionException with a unfortunately sparse message. I am using MVC1 and the corresponding MvcContirb.

[Test] public void ThisShouldNotErrorButItDoes() { "~/District/ParticipantInfo/1907/2010".Route().Values.ToList().ForEach(r => Console.WriteLine(r.Key + ": " + r.Value)); Console.WriteLine(((Route)"~/District/ParticipantInfo/1907/2010".Route().Route).Url); "~/District/ParticipantInfo/1907/2010".ShouldMapTo<DistrictController>(c => c.ParticipantInfo(1907, 2010)); } 

The first two lines indicate that an exception should not be thrown. I map the correct controller, action, district number and poll to match the correct route {controller} / {action} / {districtNumber} / {surveyYear}.

My routes:

  public static void RegisterRoutes(RouteCollection routes) { routes.IgnoreRoute("{resource}.axd/{*pathInfo}"); routes.MapRoute( "Participation", "{controller}/{action}/{districtNumber}/{surveyYear}", new { controller = "District", action = "ParticipantInfo" }); routes.MapRoute( "Default", // Route name "{controller}/{action}/{id}", // URL with parameters new { controller = "Home", action = "Index", id = string.Empty }); // Parameter defaults } 

The error I get is MvcContrib.TestHelper.AssertionException: the value of the parameter does not match.

I tracked this: public static RouteData ShouldMapTo (this action is RouteData routeData, Expression>) where TController: Controller
What is inside RouteTestingExtensions.cs

Does anyone have a clue to this?

+4
source share
1 answer

You may have already done something, but there may be others who land on this issue and do not receive any help. So I thought that I would try to give some information about the problem.

I must admit that I myself got involved with this problem. Please allow me to refer to another forum for understanding.

Jonathan McCracken (author of Test-Drive ASP.NET MVC, which I really like):

MVC Contrib does not display parameters, that is, prior to binding to the model, which can be tested separately. Therefore, in cases where you have parameters, you need to pass null, and then your test will pass.

Here is the solution:

 [TestFixture] public class RouteDefinitionsTest { [SetUp] public void setup() { var routes = RouteTable.Routes; routes.Clear(); RouteDefinitions.AddRoutes(routes); } [Test] public void Should_Route_To_Edit_Page_With_Title() { "~/Todo/Edit". ShouldMapTo<TodoController>(x => x.Edit(null)); } } 

Of course, you need to add RouteDefinitions to your web project, which is located here:

 public class RouteDefinitions { public static void AddRoutes(RouteCollection routes) { routes.IgnoreRoute("{resource}.axd/{*pathInfo}"); routes.MapRoute("Default", // Route name "{controller}/{action}/{id}", // URL with parameters new {controller = "Home", action = "Index", id = string.Empty} // Parameter defaults ); } } 

I just extracted this from the Global.asax.cs file, so now you need to change the following:

  public static void RegisterRoutes(RouteCollection routes) { RouteDefinitions.AddRoutes(routes); } 

Source (July 21, 2010): http://forums.pragprog.com/forums/124/topics/4824

If you care about the inner workings of "ShouldMapTo ()", then here is the source. Knock yourself out.

  public static RouteData ShouldMapTo<TController>(this RouteData routeData, Expression<Func<TController, ActionResult>> action) where TController : Controller { Assert.That(routeData, Is.Not.Null, "The URL did not match any route"); //check controller routeData.ShouldMapTo<TController>(); //check action var methodCall = (MethodCallExpression) action.Body; string actualAction = routeData.Values.GetValue("action").ToString(); string expectedAction = methodCall.Method.Name; actualAction.AssertSameStringAs(expectedAction); //check parameters for (int i = 0; i < methodCall.Arguments.Count; i++) { string name = methodCall.Method.GetParameters()[i].Name; object value = ((ConstantExpression) methodCall.Arguments[i]).Value; Assert.That(routeData.Values.GetValue(name), Is.EqualTo(value.ToString())); } return routeData; } 

Source (November 25, 2008): http://flux88.com/blog/fluent-route-testing-in-asp-net-mvc/ (this may be a bit outdated)

+3
source

All Articles