gpt4 book ai didi

MvcContrib ShouldMapTo TestHelper 意外抛出 AssertionException

转载 作者:行者123 更新时间:2023-12-03 09:51:48 26 4
gpt4 key购买 nike

我在使用 MvcContrib 的 ShouldMapTo 函数进行路由测试时遇到预期错误。根据结果​​,一切都很好,但助手抛出了一个 AssertionException ,不幸的是消息稀疏。我正在使用 MVC1 和相应的 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));
}

前两行表明不应抛出异常。我正在映射正确的 Controller 、操作、districtNumber 和surveyYear 以匹配{controller}/{action}/{districtNumber}/{surveyYear} 的正确路线。

我的路线:
    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
}

我得到的错误是 MvcContrib.TestHelper.AssertionException:参数值不匹配。

我已经将其追踪到: public static RouteData ShouldMapTo(this RouteData routeData, Expression> action)
其中 TController : Controller
这是在 RouteTestingExtensions.cs 里面

有没有人对此有任何线索?

最佳答案

可能您已经解决了一些问题,但可能还有其他一些人遇到了这个问题而没有得到任何帮助。所以我想我会尝试提供一些关于这个问题的信息。
不得不承认,我自己也纠结过这个问题。
请让我引用另一个论坛以获得一些见解。
Jonathan McCracken(我非常喜欢的“Test-Drive ASP.NET MVC”的作者):

MVC Contrib does not map parameters, that’s up to model binding which can be tested separately. So in these 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));
}
}

Course you’ll need to add RouteDefinitions to your Web project as well which is 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 out of the Global.asax.cs file, so now that needs to change to the following:

    public static void RegisterRoutes(RouteCollection routes)
{
RouteDefinitions.AddRoutes(routes);
}
来源(2010 年 7 月 21 日): http://forums.pragprog.com/forums/124/topics/4824
如果您关心“ShouldMapTo()”的内部工作原理,那么这里是来源。把自己打昏。
    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;
}
来源(2008 年 11 月 25 日): http://flux88.com/blog/fluent-route-testing-in-asp-net-mvc/ (可能有点过时了)

关于MvcContrib ShouldMapTo TestHelper 意外抛出 AssertionException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1891526/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com