Asp.net mvc 如何使用可选查询字符串参数测试MVC路由
我有一个控制器方法:Asp.net mvc 如何使用可选查询字符串参数测试MVC路由,asp.net-mvc,mvccontrib,attributerouting,Asp.net Mvc,Mvccontrib,Attributerouting,我有一个控制器方法: [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&ord
[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"));
“~/which/list”。应该映射到(c=>c.Index(string.Empty,string.Empty));
“~/which/list?sortby=type&order=desc”。应该映射到(c=>c.Index(“type”,“desc”));
但是,它返回此错误
失败:MvcContrib.TestHelper.AssertionException:的值
参数“sortby”不匹配:应为“”,但为“”;无价值
在名为“sortby”的路由上下文操作参数中找到-您的
匹配的路由是否包含名为“sortby”的令牌
我缺少什么?基于断言消息(
预期为“”,但为“”;
因此其中一个值是null
或string.Empty
在断言中)您的第一个测试失败,因为您使用了string.Empty
但string的默认值是null
将断言更改为使用null
,它应该:
"~/whatever/list".ShouldMapTo<MyController>(c => c.Index(null, null));
“~/which/list”。应该映射到(c=>c.Index(null,null));
我以前喜欢
var route = "~/whatever/list".WithMethod(HttpVerbs.Get);
route.Values.Add("sortby", "type");
route.Values.Add("order", "desc");
route.ShouldMapTo<MyController>(c => c.Index("type", "desc"));
var route=“~/whatever/list.WithMethod(HttpVerbs.Get);
添加(“排序”、“类型”);
添加(“订单”、“描述”);
route.ShouldMapTo(c=>c.Index(“type”,“desc”));
我认为第一个测试失败了::“~/whatever/list”。ShouldMapTo(c=>c.Index(string.Empty,string.Empty))
尝试使用null
而不是string。空的,因为null
是string
的默认值。如果您将其作为答案,我会将其标记为已回答。似乎就是这样,我不知道为什么我没想到。若索引的参数是int呢?那么您不能传递null,您如何使该测试通过(而不将控制器的方法更改为except nullable)?那么我猜您需要使用0
,因为这是int的默认值。因此您的断言看起来像“~/whatever/list”。应该映射到(c=>c.Index(0,0))代码>但如果没有看到控制器和路由配置,我无法确定这是我实际尝试的,但是控制器的操作接受int,并且断言失败,因为它改为null(我不能将其作为不可为null的int传递)
预期为“0”,但实际为“”