2

如何对 MVC 重定向进行单元测试?

public ActionResult Create(Product product)
{

    _productTask.Save(product);

    return RedirectToAction("Index",new {id = product.Id});

}

public ActionResult Index(int id)
{

    return View();
}

在这里,我使用单元测试语句作为

 var result = controller.CreateGroup(group) as RedirectToRouteResult;
    //// Assert 
    Assert.That(result, Is.Not.Null);

但它失败了结果返回空值任何人都可以帮助我

4

1 回答 1

0
// Arrange
var product = new Product(){Id=1};

// Act
var result = controller.Create(product);

// Assert
result.AssertActionRedirect()
                .ToController("thisControllerName")
                .ToAction("Index");
于 2012-11-20T04:31:24.497 回答