asp.Net   发布时间:2022-04-04  发布网站:大佬教程  code.js-code.com
大佬教程收集整理的这篇文章主要介绍了单元测试ASP.NET MVC重定向大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。
如何单元测试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团队表示他们正在写框架是可测试的。

解决方法

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

大佬总结

以上是大佬教程为你收集整理的单元测试ASP.NET MVC重定向全部内容,希望文章能够帮你解决单元测试ASP.NET MVC重定向所遇到的程序开发问题。

如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。