For what it's worth - I revised my test, my extension method similar to this (including J. Tikhon's feedback):
public static void AssertViewWasReturned(this ActionResult result, string viewName, string defaultViewName) { Assert.IsInstanceOf<ViewResultBase>(result, "Result is not an instance of ViewResultBase"); var viewResult = (ViewResultBase)result; var actualViewName = viewResult.ViewName; if (actualViewName == "") actualViewName = defaultViewName; Assert.AreEqual(viewName, actualViewName, string.Format("Expected a View named{0}, got a View named {1}", viewName, actualViewName)); }
This means that my unit tests may contain code like this:
var result = controller.MyAction(); result.AssertViewWasReturned("ExpectedViewName","MyAction")
This is not as good as I hoped, since I need to specify "defaultViewName" (ie the name of the action), but this is a reasonable amount.
source share