Unit Test to determine that Action is returning correct View

Viewed 9486

How can you test that an action method you have like so will return the correct view because Viewname is an empty string? Should I even bother testing this? I'm not sure how many unit tests to do, I'm thinking you could create a lot of unit tests!

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


[TestMethod]
public void DetermineIndexReturnsCorrectView()
{
     HomeController controller = new HomeController();

     ViewResult result = controller.Index() as ViewResult;

     //****result.ViewName is empty!!!!***//
     Assert.AreEqual("Index", result.ViewName);
}
5 Answers

If you have a view like

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

the ViewName property will take the default value "". You need to specify the ViewName like

public ActionResult Index()
{
    return View("Index");
}

to solve it.

Related