ASP.NET MVC – Create an Unit Test Project to test the Controller ActionResult
The Unit Test Project is not created with MVC Web Project at the same time.
If it is a Native Unit Test Project,
Please also Add Following Reference Namespace on the Project.
– Microsoft.CSharp
– System.Web.Mvc
– System.Web.WebPages
– System.Web.WebPages.Deployment
– System.Web.WebPages.Razor
– System.Web.Razor
– < Actual Web Application Project >
using System; using Microsoft.VisualStudio.TestTools.UnitTesting; using Web.Controllers; using System.Web.Mvc; namespace Web.Tests { [TestClass] public class AdminControllerTest { [TestMethod] public void ViewBag_ID_Value_IsNotNullTest() { AdminController controller = new AdminController(); ViewResult actionResult = controller.Index() as ViewResult; Assert.IsNotNull(actionResult.ViewBag.ID, "ID Value is null"); } } } |
– You can click the Visual Studio Top Menu => Test => Windows => Test Explorer to show the Unit Test – Test Case List.
All Test Case from Different Project under the Same Solution would also be shown on the Test Explorer.
– If you want to perform the Test Case from the Debug Mode, please select "Debug Selected Item".