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",
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)