gpt4 book ai didi

asp.net-mvc - 如何使用可选的查询字符串参数测试 MVC 路由

转载 作者:行者123 更新时间:2023-12-01 01:18:14 26 4
gpt4 key购买 nike

我有这个 Controller 方法:

[GET("/whatever/list")]
public ActionResult Index(string sortby, string order)

我正在尝试使用 MvcContrib 路由测试来测试它:
"~/whatever/list".ShouldMapTo<MyController>(c => c.Index(string.Empty, string.Empty));
"~/whatever/list?sortby=type&order=desc".ShouldMapTo<MyController>(c => c.Index("type", "desc"));

但是,它返回此错误。

Failure: MvcContrib.TestHelper.AssertionException : Value for parameter 'sortby' did not match: expected '' but was ''; no value found in the route context action parameter named 'sortby' - does your matching route contain a token called 'sortby'?



我错过了什么?

最佳答案

根据断言消息(expected '' but was ''; 所以断言中的一个值是 nullstring.Empty)你的第一个测试失败了,因为你使用了 string.Empty但字符串的默认值是 null
更改您的断言以使用 null它应该是:

"~/whatever/list".ShouldMapTo<MyController>(c => c.Index(null, null));

关于asp.net-mvc - 如何使用可选的查询字符串参数测试 MVC 路由,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10759402/

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