单元测试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团队表示他们正在写框架是可测试的。

解决方法

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

相关文章

### 创建一个gRPC服务项目(grpc服务端)和一个 webapi项目(...
一、SiganlR 使用的协议类型 1.websocket即时通讯协议 2.Ser...
.Net 6 WebApi 项目 在Linux系统上 打包成Docker镜像,发布为...
一、 PD简介PowerDesigner 是一个集所有现代建模技术于一身的...
一、存储过程 存储过程就像数据库中运行的方法(函数) 优点:...
一、Ueditor的下载 1、百度编辑器下载地址:http://ueditor....