.Net MVC Unit testing a child action

A quick demo of how to unit test a child action in with NUnit/MVC:

The controller action

public ActionResult ChildActionTest(string category)
   var result = new ChildActionModel {ChildProp = category + " This is a child action string."};
   return View("~/Views/Test/ChildActionTest.cshtml", result);

The test

public void TestController_ChildActionTest_ChildView()
   const string testCategory = "TestCategory";
   const string expectedViewName = "~/Views/Test/ChildActionTest.cshtml";

   var controller = new TestController();
   var result = controller.ChildActionTest(testCategory) as ViewResult;

   Assert.IsNotNull(result, "Should have returned a ViewResult");
   Assert.AreEqual(expectedViewName, result.ViewName, "View name should have been {0}",    expectedViewName);

Leave a Reply

Your email address will not be published. Required fields are marked *