I recently did some refactoring of my mvc application and realized that there are a lot of returned static views. Instead of having multiple controllers with the results of actions that return only the view, I decided to create one controller that returns static views if they exist, and gives a 404 error if the view does not exist.
public ActionResult Index(string name)
{
ViewEngineResult result = ViewEngines.Engines.FindView(ControllerContext, name, null);
if (result.View == null)
ThrowNotFound("Page does not exists.");
return View(name);
}
My question is, is this the right way to unit test? I tried the following code, but the error I get is "RouteData should contain an element named" controller "with a non-empty string value."
[Theory]
[InlineData("ContactUs")]
public void Index_should_return_view_if_view_exists(string name)
{
controller = new ContentController();
httpContext = controller.MockHttpContext("/", "~/Content/Index", "GET"); ;
var result = (ViewResult)controller.Index(name);
Assert.NotNull(result.View);
}
, unit test . , ViewEngines SetupGet FindView , , , null.
? , .