4

我有这个控制器方法:

[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"));

但是,它返回此错误。

失败:MvcContrib.TestHelper.AssertionException:参数“sortby”的值不匹配:预期为“”但为“”;在名为“sortby”的路由上下文操作参数中找不到值 - 您的匹配路由是否包含名为“sortby”的标记?

我错过了什么?

4

2 回答 2

2

根据断言消息(expected '' but was '';因此其中一个值是nullstring.Empty在断言中)您的第一个测试失败,因为您使用string.Empty但字符串的默认值是null

更改要使用的断言null,它应该可以:

"~/whatever/list".ShouldMapTo<MyController>(c => c.Index(null, null));
于 2012-05-25T18:27:09.437 回答
2

我用过

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"));
于 2013-07-08T13:39:59.170 回答