I have a really simple action in a controller that simply returns a view which only contains html.
public class TestController : Controller
{
public ActionResult Index()
{
return View();
}
}
How can I make sure that the view renders without an error? When I execute this:
UsersController controller = new UsersController( );
ViewResult result = controller.Index( ) as ViewResult;
Assert.IsNotNull( result );
it passes but everything within the result object (ie. MasterName and ViewName) are empty.
Is there any way to test this?
If you want to test template you have to execute
ExecuteResultof theViewResult. The problem isControllerContextthat you have to pass to this method. You have to mock too many stuff. You can look for test helpers in MvcContrib. They prebuilt some mocks.On the other hand, probably this is not good idea. In most cases it’s better to cover such stuff with integration tests. Even simple request to the url will give confidence that template is successfully compiled.