0

apologized to post this question here but i am in problem suddenly because i need to write unit test code for action where i am not good.

i am bit familiar with asp.net mvc. before i never write unit test code for action rather i manually test the action. now i want to know the art of writing unit test code for my action. so i read couple of article on unit test but notice all article share very basic idea about to write unit test code for action method in mvc controller but i am not being able to write unit test code for my action. so here i am pasting my one sample controller and their actions. so anyone please share knowledge how to write unit test code for my action methods below. if possible discuss with code sample or good hint which enable to write unit test code in VS2013 and MVC version 5.

here is my code

public class StudentController : Controller
    {
        private StudentRepository _Studentdata;
        private StateRepository _Statedata;
        private CityRepository _Citydata;

        public StudentController()
        {

            _Studentdata = new StudentRepository(System.Configuration.ConfigurationManager.ConnectionStrings["StudentDBContext"].ConnectionString);
            _Statedata = new StateRepository(System.Configuration.ConfigurationManager.ConnectionStrings["StudentDBContext"].ConnectionString);
            _Citydata = new CityRepository(System.Configuration.ConfigurationManager.ConnectionStrings["StudentDBContext"].ConnectionString);

            //_Studentdata = new StudentRepository(System.Configuration.ConfigurationManager.ConnectionStrings["StudentSQLDBContext"].ConnectionString);
            //_Statedata = new StateRepository(System.Configuration.ConfigurationManager.ConnectionStrings["StudentSQLDBContext"].ConnectionString);
            //_Citydata = new CityRepository(System.Configuration.ConfigurationManager.ConnectionStrings["StudentSQLDBContext"].ConnectionString);

        }

        // GET: Stuent
        public ActionResult List(StudentListViewModel oSVm)
        {
            if (Request.IsAjaxRequest())
                System.Threading.Thread.Sleep(1000); // just simulate delay of one second

            StudentListViewModel SVm = new StudentListViewModel();
            SVm.SetUpParams(oSVm);
            SVm.Students = _Studentdata.GetStudents(oSVm.page, oSVm.PageSize, oSVm.sort, oSVm.sortdir).ToList();
            SVm.States = _Statedata.GetAll().ToList();
            SVm.Cities = _Citydata.GetAll().ToList();
            SVm.RowCount = _Studentdata.DataCounter;
            return View("ListStudents",SVm);
        }

        [HttpPost]
        public ActionResult UpdateStudents(StudentListViewModel oSVm, string Action)
        {
            if (Request.IsAjaxRequest())
                System.Threading.Thread.Sleep(1000); // just simulate delay of one second

            StudentListViewModel SVm = new StudentListViewModel();
            SVm.SetUpParams(oSVm);
            if (Action == "UPDATE")
            {
                SVm.Students = _Studentdata.SaveXML(new List<Student>(oSVm.Students).ToXml("Students"),
                    oSVm.page, oSVm.PageSize, oSVm.sort, oSVm.sortdir).ToList();
            }
            else if (Action == "DELETE")
            {
                SVm.Students = _Studentdata.Delete(oSVm.Students[0].ID,
                    oSVm.page, oSVm.PageSize, oSVm.sort, oSVm.sortdir).ToList();

            }

            SVm.States = _Statedata.GetAll().ToList();
            SVm.Cities = _Citydata.GetAll().ToList();
            SVm.RowCount = _Studentdata.DataCounter;
            return PartialView("_StudentGrid", SVm);
        }

        [HttpPost]
        public ActionResult RefreshStudents(StudentListViewModel oSVm)
        {
            if (Request.IsAjaxRequest())
                System.Threading.Thread.Sleep(1000); // just simulate delay of one second

            StudentListViewModel SVm = new StudentListViewModel();
            SVm.SetUpParams(oSVm);
            SVm.Students = _Studentdata.GetStudents(oSVm.page, oSVm.PageSize, oSVm.sort, oSVm.sortdir).ToList();
            SVm.States = _Statedata.GetAll().ToList();
            SVm.Cities = _Citydata.GetAll().ToList();
            SVm.RowCount = _Studentdata.DataCounter;
            return PartialView("_StudentGrid", SVm);
        }

        [HttpGet]
        public JsonResult GetCityName(int StateID)
        {
            if (Request.IsAjaxRequest())
                System.Threading.Thread.Sleep(1000); // just simulate delay of one second

            return Json(new {CityList =_Citydata.GetCityByStateId(StateID)} , JsonRequestBehavior.AllowGet);
        }

    }

Thanks

Thomas
  • 33,544
  • 126
  • 357
  • 626
  • 1
    What is it you wanting to test? And you need to start with a mocking framework (for example [Moq](https://github.com/Moq/moq4/wiki/Quickstart)) so that you can mock your database context –  Mar 18 '16 at 11:15
  • do i need to mock for all my action or few specific actions? – Thomas Mar 18 '16 at 11:36
  • @StephenMuecke can u please write test code for one action only so i can try for the rest because i need good hint. i read few article on the same but still not being able to write test code. please help me. thanks – Thomas Mar 18 '16 at 11:40
  • Its not the action you need to mock, its your database context and `ControllerContext`. What is it that you want to test? –  Mar 18 '16 at 11:40
  • here i paste few action like `List ,UpdateStudents,GetCityName` etc and every action has some code and for which i need to write unit test code. so i am looking for some help which enable me to start write unit test code or give me hint etc. – Thomas Mar 18 '16 at 11:46
  • Its late but I'll add some more info tomorrow (although I suspect the question will be closed as to broad) –  Mar 18 '16 at 11:53
  • @StephenMuecke please answer tomorrow that will really help me to learn and write unit test code for my action. some one close it though. – Thomas Mar 18 '16 at 12:07
  • @StephenMuecke one more request. please have look at this post too where i wrote little code about unit test just tell me that is right or not. http://stackoverflow.com/questions/36084197/asp-net-mvc-unit-test-code-when-working-with-validationattribute-and-iclientval – Thomas Mar 18 '16 at 13:12
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/106773/discussion-between-stephen-muecke-and-thomas). –  Mar 19 '16 at 01:50
  • @StephenMuecke if possible please write unit test code for at least action method. my all action code i posted here. your guide will give me good start. thanks – Thomas Mar 19 '16 at 08:51
  • Did you go into chat and read all the information I gave you? –  Mar 19 '16 at 08:55
  • @StephenMuecke thanks i just notice....it will be good start for me but i am not familiar with ninject rather i am using visual studio own unit test framework. – Thomas Mar 19 '16 at 08:59
  • You still use the VS unit testing framework. Ninject is just a Dependency Injection framework so you can inject dependencies (such as a database context into your controller) so that you can then mock it. –  Mar 19 '16 at 09:01

0 Answers0