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

单元测试ASP.NET MVC重定向

发布时间:2020-12-16 00:27:11 所属栏目:asp.Net 来源:网络整理
导读:如何单元测试MVC重定向? public ActionResult Create(Product product){ _productTask.Save(product); return RedirectToAction("Success"); }public ActionResult Success(){ return View();} Ayende’s方法仍然是最好的方法,预览5: public static void
如何单元测试MVC重定向?
public ActionResult Create(Product product)
{
    _productTask.Save(product);
    return RedirectToAction("Success");   
}

public ActionResult Success()
{ 
     return View();
}

Ayende’s方法仍然是最好的方法,预览5:

public static void RenderView(this Controller self,string action) 
 {
    typeof(Controller).GetMethod("RenderView").Invoke(self,new object[] { action} ); 
 }

似乎很奇怪,要做到这一点,特别是当MVC团队表示他们正在写框架是可测试的。

解决方法

[TestFixture]
public class RedirectTester
{
    [Test]
    public void Should_redirect_to_success_action()
    {
        var controller = new RedirectController();
        var result = controller.Index() as RedirectToRouteResult;
        Assert.That(result,Is.Not.Null);
        Assert.That(result.Values["action"],Is.EqualTo("success"));
    }
}

public class RedirectController : Controller
{
    public ActionResult Index()
    {
        return RedirectToAction("success");
    }
}

(编辑:李大同)

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

    推荐文章
      热点阅读