asp.net-mvc – 使用单元测试在asp.net mvc中验证重定向
发布时间:2020-12-15 23:27:48 所属栏目:asp.Net 来源:网络整理
导读:在单元测试中是否有一种简单的方法来验证控制器动作确实重定向到特定页面? 控制器代码: public ActionResult Create(ProductModel newProduct){ this.repository.CreateProduct(newProduct); return RedirectToAction("Index");} 所以在我的测试中,我需要
在单元测试中是否有一种简单的方法来验证控制器动作确实重定向到特定页面?
控制器代码: public ActionResult Create(ProductModel newProduct) { this.repository.CreateProduct(newProduct); return RedirectToAction("Index"); } 所以在我的测试中,我需要验证控制器实际上是重定向. ProductController controller = new ProductController(repository); RedirectToRouteResult result = (RedirectToRouteResult)controller.Create(newProduct); bool redirected = checkGoesHere; Assert.True(redirected,"Should have redirected to 'Index'"); 我只是不知道如何做验证.有任何想法吗? 解决方法
当然:
Assert.AreEqual("Index",result.RouteValues["action"]); Assert.IsNull(result.RouteValues["controller"]); // means we redirected to the same controller 并使用MvcContrib.TestHelper,您可以以更优雅的方式编写本机测试(您甚至不需要投射到RedirectToRouteResult): // arrange var sut = new ProductController(repository); // act var result = sut.Create(newProduct); // assert result .AssertActionRedirect() .ToAction("Index"); (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |
相关内容
推荐文章
站长推荐
- asp.net-mvc – 如何通过Resharper 8.2.1停止INC
- asp.net-core – 虚拟目录中的IIS站点Swagger UI
- asp.net-web-api – Structure Map无参数构造函数
- asp.net-mvc – NHibernate与实体框架
- asp-classic – 升级Classic ASP WebApp的最佳方
- asp.net-mvc – MVC(ASP.NET MVC)乐队3层架构如何
- 如何在asp.net mvc项目中使用Material-UI
- asp.net-mvc – 发布MVC应用程序VS2013时未包含字
- asp.net – 如何使用MVC Razor设置类等html属性?
- asp.net core 3.0 中使用 swagger
热点阅读