jeudi 21 mai 2015

How to pass Model value from Test class (Nunit) to Action Method in Controller class

I am creating a Nunit testing program for MVC application. Now am writing a test case for a Methods in Controller class. I am using Nsubstitute for Mocking the object.

Just now i am learning about Nunit and Nsubstitue. I don't know, how to pass Model value which was mocked in testcase method to controller method.

Below is my method in controller class :

        public ActionResult Manage(string id)
        {
            var clusterCollections = ReadXml();
            int clusterIndex = clusterCollections.ClusterCollectionList.FindIndex(a => a.ClusterId == id);
            var model = new ClusterManagementModel()
            {
                ClusterNodeDetailsList = BindClusterDetailsToGrid(id),
                DropDownListClusterName = BindClusterNameToDropDown(),
                CurrentClusterId = clusterIndex,
                CurrentClusterName = id,
                HStatus = Hstatus(id),
                IStatus = Istatus(id)
            };
            return View(model);
        }

Below is the test case i have written:

        [TestCase]
        public void TestManage()
        {         
            var ManagementController = Substitute.ForPartsOf<ClusterManagementController>();
            var ManagementModel = Substitute.ForPartsOf<ClusterManagementModel>();
            ClusterCollections clusterCollection = new ClusterCollections();
            List<ClusterNodeDetails> ClusterNodes = new List<ClusterNodeDetails>();
            List<DDL_ClusterName> DropDownListClusterName = new List<DDL_ClusterName>();
            ManagementController.ReadXml().Returns(clusterCollection);
            ManagementModel = new ClusterManagementModel()
            {
                ClusterNodeDetailsList = ClusterNodes,
                DropDownListClusterName = DropDownListClusterName,
                CurrentClusterId = 1,
                CurrentClusterName = "UnitTesting",
                HStatus = "True",
                IStatus = "Success"
            };
            var result = ManagementController.Manage("1") as ActionResult;
            Assert.AreEqual(ManagementModel, result);
        }

If i have done mistake in testcase Method please correct me.

If my testcase is wrong give me a suggestion h ow to write a testcase for the above method (public ActionResult Manage(string id))

Aucun commentaire:

Enregistrer un commentaire