Unit testing Net寻找如何为MVC操作单元测试代码的指导

Unit testing Net寻找如何为MVC操作单元测试代码的指导,unit-testing,asp.net-mvc-5,visualstudio.testtools,Unit Testing,Asp.net Mvc 5,Visualstudio.testtools,很抱歉在这里发布这个问题,但我突然遇到了问题,因为我需要为我不擅长的地方编写单元测试代码 我对asp.NETMVC有点熟悉。在此之前,我从不为操作编写单元测试代码,而是手动测试操作。现在我想知道为我的操作编写单元测试代码的艺术。所以我读了几篇关于单元测试的文章,但是注意到所有的文章都分享了关于在mvc控制器中为动作方法编写单元测试代码的基本想法,但是我不能为我的动作编写单元测试代码。所以我在这里粘贴我的一个示例控制器及其操作。因此,请大家分享如何为我下面的操作方法编写单元测试代码的知识。如果可能

很抱歉在这里发布这个问题,但我突然遇到了问题,因为我需要为我不擅长的地方编写单元测试代码

我对asp.NETMVC有点熟悉。在此之前,我从不为操作编写单元测试代码,而是手动测试操作。现在我想知道为我的操作编写单元测试代码的艺术。所以我读了几篇关于单元测试的文章,但是注意到所有的文章都分享了关于在mvc控制器中为动作方法编写单元测试代码的基本想法,但是我不能为我的动作编写单元测试代码。所以我在这里粘贴我的一个示例控制器及其操作。因此,请大家分享如何为我下面的操作方法编写单元测试代码的知识。如果可能的话,讨论代码示例或好的提示,使您能够在VS2013和MVC版本5中编写单元测试代码

这是我的密码
公共类学生控制器:控制器
{
私人学生调查(学生数据);;
私有状态存储库_Statedata;
私人城市存储库(城市数据);;
公共学生控制员()
{
_Studentdata=new StudentRepository(System.Configuration.ConfigurationManager.ConnectionString[“StudentDBContext”].ConnectionString);
_Statedata=新的StateRepository(System.Configuration.ConfigurationManager.ConnectionString[“StudentDBContext”].ConnectionString);
_Citydata=new CityRepository(System.Configuration.ConfigurationManager.ConnectionStrings[“StudentDBContext”].ConnectionString);
//_Studentdata=newstudentrepository(System.Configuration.ConfigurationManager.ConnectionString[“StudentSQLDBContext”].ConnectionString);
//_Statedata=新的StateRepository(System.Configuration.ConfigurationManager.ConnectionString[“StudentSQLDBContext”].ConnectionString);
//_Citydata=new CityRepository(System.Configuration.ConfigurationManager.ConnectionStrings[“StudentSQLDBContext”].ConnectionString);
}
//得到:斯图尔特
公共操作结果列表(StudentListViewModel oSVm)
{
if(Request.IsAjaxRequest())
System.Threading.Thread.Sleep(1000);//只需模拟一秒钟的延迟
StudentListViewModel SVm=新的StudentListViewModel();
设置参数(oSVm);
SVm.Students=_Studentdata.GetStudents(oSVm.page,oSVm.PageSize,oSVm.sort,oSVm.sortdir).ToList();
States=_Statedata.GetAll().ToList();
SVm.Cities=_Citydata.GetAll().ToList();
SVm.RowCount=\u Studentdata.DataCounter;
返回视图(“ListStudents”,SVm);
}
[HttpPost]
公共操作结果更新学生(学生列表视图模型oSVm,字符串操作)
{
if(Request.IsAjaxRequest())
System.Threading.Thread.Sleep(1000);//只需模拟一秒钟的延迟
StudentListViewModel SVm=新的StudentListViewModel();
设置参数(oSVm);
如果(操作==“更新”)
{
SVm.Students=_Studentdata.SaveXML(新列表(oSVm.Students).ToXml(“学生”),
oSVm.page、oSVm.PageSize、oSVm.sort、oSVm.sortdir.ToList();
}
else if(操作==“删除”)
{
SVm.Students=\u Studentdata.Delete(oSVm.Students[0].ID,
oSVm.page、oSVm.PageSize、oSVm.sort、oSVm.sortdir.ToList();
}
States=_Statedata.GetAll().ToList();
SVm.Cities=_Citydata.GetAll().ToList();
SVm.RowCount=\u Studentdata.DataCounter;
返回部分视图(“学生网格”,SVm);
}
[HttpPost]
公共行动结果刷新学生(StudentListViewModel oSVm)
{
if(Request.IsAjaxRequest())
System.Threading.Thread.Sleep(1000);//只需模拟一秒钟的延迟
StudentListViewModel SVm=新的StudentListViewModel();
设置参数(oSVm);
SVm.Students=_Studentdata.GetStudents(oSVm.page,oSVm.PageSize,oSVm.sort,oSVm.sortdir).ToList();
States=_Statedata.GetAll().ToList();
SVm.Cities=_Citydata.GetAll().ToList();
SVm.RowCount=\u Studentdata.DataCounter;
返回部分视图(“学生网格”,SVm);
}
[HttpGet]
公共JsonResult GetCityName(int StateID)
{
if(Request.IsAjaxRequest())
System.Threading.Thread.Sleep(1000);//只需模拟一秒钟的延迟
返回Json(新的{CityList=_Citydata.GetCityByStateId(StateID)},JsonRequestBehavior.AllowGet);
}
}

谢谢

你想测试什么?你需要从一个模拟框架(例如)开始,这样你就可以模拟你的数据库上下文我需要模拟我所有的动作还是几个特定的动作?@StephenMuecke你能不能只为一个动作编写测试代码,这样我就可以试着去做其余的动作,因为我需要好的提示。我读了几篇同样的文章,但仍然不能编写测试代码。请帮帮我。这不是您需要模拟的操作,而是您的数据库上下文和
ControllerContext
。你想测试什么?这里我粘贴了一些动作,比如
List、UpdateStudents、GetCityName
等等,每个动作都有一些代码,我需要为它们编写单元测试代码。因此,我正在寻找一些帮助,使我能够开始编写单元测试代码或给我提示等。
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);
        }

    }