2012-01-14 57 views
3

我是單元測試領域的新手,所以我有一些問題:如何測試返回DataTable的方法?

是否讓一個返回DataTable的方法成爲一個好習慣?如果是,那麼我怎麼測試它呢?

這是他們問我要測試的函數:

public static DataTable GeneratePeriods(DateTime startDate, DateTime endDate, string periodicity) 
{ 
    try 
    { 
     using (DataTable periods = new DataTable()) 
     { 
      periods.Columns.Add(new DataColumn("Dates", typeof(DateTime))); 
      int startYear = startDate.Year; 
      int endYear = endDate.Year; 
      int Diff = endYear - startYear + 1; 
      int LoopYear; 
      /* This generates all regular periods from the begin date to the end date */ 
      switch (periodicity) 
      { 
       //monthly 
       case "Mensuelle": 
        for (int j = 0; j < Diff; j++) 
        { 
         LoopYear = startYear + j; 
         for (int i = 1; i <= 12; i++) 
          if (i != 12) 
           periods.Rows.Add(new DateTime(LoopYear, i + 1, 1).AddDays(-1)); 
          else 
           periods.Rows.Add(new DateTime(LoopYear, 12, 31)); 
        } 
        break; 
       //quarterly 
       case "Trimestrielle": 
        for (int j = 0; j < Diff; j++) 
        { 
         LoopYear = startYear + j; 
         for (int i = 1; i <= 4; i++) 
          if (i != 4) 
           periods.Rows.Add(new DateTime(LoopYear, (i * 3) + 1, 1).AddDays(-1)); 
          else 
           periods.Rows.Add(new DateTime(LoopYear, 12, 31)); 
        } 
        break; 
       //biannual 
       case "Semestrielle": 
        for (int j = 0; j < Diff; j++) 
        { 
         LoopYear = startYear + j; 
         for (int i = 1; i <= 2; i++) 
          if (i != 2) 
           periods.Rows.Add(new DateTime(LoopYear, (i * 6) + 1, 1).AddDays(-1)); 
          else 
           periods.Rows.Add(new DateTime(LoopYear, 12, 31)); 
        } 
        break; 
       //annual 
       case "Annuelle": 
        for (int j = 0; j < Diff; j++) 
        { 
         LoopYear = startYear + j; 
         for (int i = 1; i <= 1; i++) 
          periods.Rows.Add(new DateTime(LoopYear, 12, 31)); 
        } 
        break; 
      } 
      //this adds startDate in periods datatable if it doesn't exist 
      if (periods.Select(String.Format("Dates = '{0}'", startDate)).Length == 0) 
       periods.Rows.Add(startDate); 
      //this adds endDate date in periods datatable if it doesn't exist 
      if (periods.Select(String.Format("Dates = '{0}'", endDate.AddDays(-1))).Length == 0) 
       periods.Rows.Add(endDate); 
      //this removes all date ranges below the startDate 
      DataRow[] dr = periods.Select(String.Format("Dates < '{0}'", startDate)); 
      foreach (DataRow row in dr) 
       periods.Rows.Remove(row); 
      //this removes all date ranges above the endDate 
      DataRow[] dr1 = periods.Select(String.Format("Dates >'{0}'", endDate.AddDays(-1))); 
      foreach (DataRow row in dr1) 
       periods.Rows.Remove(row); 
      //this adds endDate date in periods datatable if it doesn't exist for the second time ! (I personnaly don't know why it's duplicated but it dosen't work without this =)) 
      if (periods.Select(String.Format("Dates = '{0}'", endDate.AddDays(-1))).Length == 0) 
       periods.Rows.Add(endDate); 
      DataView dv = new DataView(periods) { Sort = "Dates ASC" }; 
      // this initialize a new datatable with sorted dates 
      DataTable dt_dates = dv.ToTable(); 
      // this initialize a new datatable 
      DataTable dt_periods = new DataTable(); 
      dt_periods.Columns.Add("Periods", typeof(string)); 
      dt_periods.Columns.Add("NombreJours", typeof(int)); 
      // this loop creates period ranges shown to the user (Du startDate au endDate) 
      DateTime dateDebutPeriode; 
      DateTime dateFinPeriode; 
      int NombreJours; 
      for (int i = 0; i < dv.Table.Rows.Count - 1; i++) 
       if (i == 0) 
       { 
        dateDebutPeriode = DateTime.Parse(dt_dates.Rows[i]["Dates"].ToString()); 
        dateFinPeriode = DateTime.Parse(dt_dates.Rows[i + 1]["Dates"].ToString()); 
        NombreJours = dateFinPeriode.Subtract(dateDebutPeriode).Days + 1; 
        dt_periods.Rows.Add(String.Format("Du {0} au {1}", dateDebutPeriode.ToShortDateString(), dateFinPeriode.ToShortDateString()), NombreJours); 
       } 
       else 
        if (i == dv.Table.Rows.Count - 2) 
        { 
         dateDebutPeriode = DateTime.Parse(dt_dates.Rows[i]["Dates"].ToString()).AddDays(1); 
         dateFinPeriode = DateTime.Parse(dt_dates.Rows[i + 1]["Dates"].ToString()).AddDays(-1); 
         NombreJours = dateFinPeriode.Subtract(dateDebutPeriode).Days + 1; 
         dt_periods.Rows.Add(String.Format("Du {0} au {1}", dateDebutPeriode.ToShortDateString(), dateFinPeriode.ToShortDateString()), NombreJours); 
        } 
        else 
        { 
         dateDebutPeriode = DateTime.Parse(dt_dates.Rows[i]["Dates"].ToString()).AddDays(1); 
         dateFinPeriode = DateTime.Parse(dt_dates.Rows[i + 1]["Dates"].ToString()); 
         NombreJours = dateFinPeriode.Subtract(dateDebutPeriode).Days + 1; 
         dt_periods.Rows.Add(String.Format("Du {0} au {1}", dateDebutPeriode.ToShortDateString(), dateFinPeriode.ToShortDateString()), NombreJours); 
        } 
      return dt_periods; 
     } 
    } 
    catch (InvalidOperationException) 
    { 
     throw; 
    } 
} 
+1

請不要在一個問題中提出多個問題。分別詢問他們。然而,在這種情況下,你的第二個問題不是一個建設性的問題,並且很可能會被關閉。 – ChrisF 2012-01-14 17:39:58

+0

也許問一個問題'我怎樣才能單元測試這個方法返回一個DataTable',提供一些代碼並從那裏取出。 – blank 2012-01-14 17:53:35

回答

0

可以測試在這裏是什麼,是返回DataTable結構是否確實是你所期望的。這些都是簡單而簡單的單元測試。確定什麼預計GeneratePeriods方法(如,你想要它做什麼?),並編寫測試,檢查它實際上是什麼方法。例如:

  • 測試是否發生時期的正確數量
  • 測試特定時期是否已正確設置行/列行數據
  • 測試是否正確格式
  • ...,可能很多更多

在附註中,從函數返回DataTable沒有任何問題。