2013-08-25 186 views
-1

喜上單元測試的預期值我是新的,我寫這個定位單元測試方法,但我不知道什麼是它的預期值。應該是什麼在單元測試

下面是要測試的功能:

public static string GetStringResource(string language, string name) 
    { 
     Localization cfg = GetInstance(); 
     try 
     { 
      if (cfg != null && cfg._config != null) 
       return cfg._config.GetValue(language, name).ToString(); 
      return string.Empty; 
     } 
     catch { return string.Format("Localization Error: '{0}:{1}' Not Found.", language, name); } 
    } 

這裏是getValue函數:

public override object GetValue(string section, string entry) 
    { 
     VerifyAndAdjustSection(ref section); 
     VerifyAndAdjustEntry(ref entry); 

     try 
     { 
      //XmlDocument doc = GetXmlDocument(); 
      XmlElement root = doc.DocumentElement; 

      XmlNode entryNode = root.SelectSingleNode(GetSectionsPath(section) + "/" + GetEntryPath(entry)); 
      return entryNode.InnerText; 
     } 
     catch { return null; } 
    } 

這是我的未完成的測試方法:

public void GetStringResourceTest() 
    { 
     string language = "English"; // TODO: Initialize to an appropriate value 
     string name = "John Smith"; // TODO: Initialize to an appropriate value 
     string expected = language; // TODO:" Initialize to an appropriate value 
     string actual; 
     actual = Localization.GetStringResource(language, name); 
     Assert.AreEqual(expected, actual); 

    } 
+1

首先,什麼語言?其次,如果你不知道這段代碼的行爲,爲什麼你會期待陌生人呢?編寫單元測試是關於給定某些輸入的測試行爲。 – Makoto

回答

1

當你正在進行單元測試時,你試圖驗證,對於給定的一組輸入,你有正確的輸出。

在這種情況下,您有兩個輸入:languagename

據推測,考慮到兩者的某種組合,有一個預期的輸出。作爲開發人員,您應該有必要的工具集來確定並驗證它是否正確。然後,一旦你寫了一個測試,你永遠不會再手動驗證該行爲!

例如,如果你的單元測試一個計算器,你可能會斷言,鑑於1+1測試Add方法,輸出爲2

+0

我沒有寫出原始代碼,所以預期的輸出在這裏有點混亂。至少我應該找出什麼路徑? –

+0

如果可能的話,與任何人編寫原始代碼交談。如果沒有,與另一位瞭解更多的團隊成員交談。 *失敗*,請與其中一位商業用戶和/或產品所有者交談。 –