01/10/2018, 09:25

Nhờ mọi người chỉ giúp Viết Unit Test view Index cho hàm sau trong asp.net mvc?

  • List item
    Controller:

    public ActionResult Index()
    {
    scm = new ServerCodeManager(ctrdata);
    Models.QCReserveModels models = new Models.QCReserveModels();

              //get List CCN
              models.colNameList = GetDataCCN();
              //get List Mas_loc_start
              models.masterList = GetDataMasLoc();
              return View(models);
    
          }
    

    dbaccsess:
    public class QCReserveModels : DbConnectionBase
    {
    public QCReserveModels() : base()
    {
    }

          public List<ComboBoxOption> GetMasLocList()
          {
              ICMLog.EventLog("Get master location data.", GetType().Name, MethodBase.GetCurrentMethod().Name);
              List<ComboBoxOption> masLocList = new List<ComboBoxOption>();
    
              var masLocModel = new ComboBoxOption();
              masLocModel.Key = Constants.DEFAULT_MAS_LOC_VAL;
              masLocModel.Value = Constants.DEFAULT_MAS_LOC_VAL;
              masLocList.Add(masLocModel);
    
              try
              {
                  var ds = serverCodeManager.GetCmbListOfMAS_LOC(ctrlData.CCN);
                  foreach (DataRow dr in ds.Tables[0].Rows)
                  {
                      masLocModel = new ComboBoxOption();
                      masLocModel.Key = dr[MAS_LOCDefine.COL_MAS_LOC].ToString();
                      masLocModel.Value = dr[MAS_LOCDefine.COL_ADD_MAS_LOC_MLOC_DESC].ToString();
                      masLocList.Add(masLocModel);
                  }
              }
              catch (Exception e)
              {
                  ICMLog.ErrorLog(e.Message, GetType().Name, MethodBase.GetCurrentMethod().Name);
              }
    
              return masLocList;
          }
    
    
          public List<ComboBoxOption> GetListCCN()
          {
               ICMLog.EventLog("Get CCN data.", GetType().Name, MethodBase.GetCurrentMethod().Name);
              List<ComboBoxOption> ccnList = new List<ComboBoxOption>();
    
              var CCNModel = new ComboBoxOption();
              CCNModel.Key = Constants.DEFAULT_MAS_LOC_VAL;
              CCNModel.Value = Constants.DEFAULT_MAS_LOC_VAL;
              ccnList.Add(CCNModel);
    
              try
              {
                  var ds = serverCodeManager.GetCmbListOfCCN();
                  foreach (DataRow dr in ds.Tables[0].Rows)
                  {
                      CCNModel = new ComboBoxOption();
                      CCNModel.Key = dr[CCNDefine.COL_CCN].ToString();
                      CCNModel.Value = dr[CCNDefine.COL_ADD_CCN_NAME].ToString();
                      ccnList.Add(CCNModel);
                  }
              }
              catch (Exception e)
              {
                  ICMLog.ErrorLog(e.Message, GetType().Name, MethodBase.GetCurrentMethod().Name);
              }
    
              return ccnList;
          }
      }
    

models:

 public class QCReserveModels
    {
        public string ID { get; set; }
        public string ColAddCNNName { get; set; }
        public string Name { get; set; }

        [Required(ErrorMessage = "bắt buộc")]
        public string TextNumber { get; set; }

        public string Masterlocation { get; set; }
        public List<ComboBoxOption> masterList { get; set; }
        public List<ComboBoxOption> colNameList { get; set; }
    }

Tôi đã viết test cho nó như sau:

 public void QCReseverIndexTest()
        {
              List<ICM.WebAP.QC.Models.QCReserveModels> qcres = null;
            qcres = new List<ICM.WebAP.QC.Models.QCReserveModels>();
            ICM.WebAP.QC.Models.QCReserveModels qcres1 = null;
            qcres1 = new ICM.WebAP.QC.Models.QCReserveModels { ID = "ID", ColAddCNNName = "ghgh", Name = "NA", TextNumber = "NA", Masterlocation = "local", };
     
            qcres = new List<ICM.WebAP.QC.Models.QCReserveModels>
                {
                    qcres1


                };
       
            var controller = new QCReservesController();
        
            ViewResult rest = controller.Index() as ViewResult;
            List<ICM.WebAP.QC.Models.QCReserveModels> models = new List<ICM.WebAP.QC.Models.QCReserveModels>();
      

        }

kết quả chạy ra là test failed mong mọi người sửa giúp để hàm test được chính xác
lỗi thông báo:

      But was:  <empty>
    " ```
Bài liên quan
0