加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 编程开发 > asp.Net > 正文

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

发布时间:2020-12-16 06:59:31 所属栏目:asp.Net 来源:网络整理
导读:我有这个控制器方法: [GET("/whatever/list")]public ActionResult Index(string sortby,string order) 我正在尝试使用MvcContrib路由测试来测试它: "~/whatever/list".ShouldMapToMyController(c = c.Index(string.Empty,string.Empty));"~/whatever/list
我有这个控制器方法:

[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’?

我错过了什么?

解决方法

基于断言消息(预期”但是”;因此其中一个值为null或断言中的string.Empty)您的第一个测试失败,因为您使用了string.Empty但是字符串的默认值为null

将你的断言更改为使用null,它应该wotk:

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

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读