Verify redirect with unit test in asp.net mvc

Viewed 14611

Is there an easy way to verify in a unit test that a controller action is indeed redirecting to a specific page?

Controller code:

public ActionResult Create(ProductModel newProduct)
{
    this.repository.CreateProduct(newProduct);
    return RedirectToAction("Index");
}

So in my test, I would need to verify that the controller is actually redirecting.

ProductController controller = new ProductController(repository);

RedirectToRouteResult result = (RedirectToRouteResult)controller.Create(newProduct);

bool redirected = checkGoesHere;
Assert.True(redirected, "Should have redirected to 'Index'");

I'm just not sure how to do the verification. Any ideas?

2 Answers
Related