2014-10-20 31 views
0

我的單元測試在c#中一直保持失敗,我嘗試了幾種不同的方法。任何幫助將不勝感激。這只是不轉換我添加到小寫的書。所以測試失敗單元測試未能在c中將字符串列表轉換爲小寫#

private List<string> _number; 

    public Book (string[] id) 
    { 
     //_number = idents.Select (d => d.ToLower()).ToList(); 

     _number = new List<string>(id); 
     _number = _number.ConvertAll (d => d.ToLower()); 
    } 

    public bool Exist (string id) 
    { 
     return _number.Contains (id); 
    } 

    public void AddBook (string id) 
    { 
     _number.Add (id.ToLower()); 
    } 
    _______________________________________________________________________________ 

    [Test()] 
    public void TestAddBook() 
    { 
     Book id = new Book (new string[] {"ABC", "DEF"}); 
     id.AddBook ("GHI"); 

     Assert.AreEqual (true, id.Exist ("ghi")); 
    } 
+0

你在AddIdentifier中將它轉換爲小寫嗎? – artm 2014-10-20 01:23:23

+0

不知道爲什麼這是倒投了,似乎是一個合法的問題。 – 2014-10-20 01:24:09

+0

聲明瞭「_name」變量和「AreYou」方法在哪裏? – TheVillageIdiot 2014-10-20 01:29:36

回答

1

不應該TestMethod的是這樣的:

[TestMethod] 
public void TestAddBook() 
{ 
    Book id = new Book (new string[] {"ABC", "DEF"}); 
    id.AddBook ("GHI"); 

    Assert.AreEqual (true, id.Exist ("ghi")); 
} 

這至少是我的psycic水晶球的感覺。

+0

但是可能是OP使用他們自己的定製測試框架:) – TheVillageIdiot 2014-10-20 01:47:47

+0

另一個值得一提的好消息是他沒有使用標準測試框架作爲shiped – 2014-10-20 01:52:49

1

解決此問題的更好方法實際上並不是將鍵轉換爲小寫,而是使用可以以不區分大小寫的方式存儲鍵的構造。這將節省處理時間並減少編程錯誤。

如果你有興趣存儲書本密鑰,那麼我強烈建議使用HashSet代替。

列表的Contains method is O(n)Hashset's is O(1)。如果你有很多條目,這是一個重要的區別。

下面是一個使用HashSet的書類的重寫:

public class Book 
{ 
    private HashSet<string> _number; 

    public Book(string[] id) 
    { 
     _number = new HashSet<string>(id, StringComparer.InvariantCultureIgnoreCase); 
    } 

    public bool Exist(string id) 
    { 
     return _number.Contains(id); 
    } 

    public void AddBook(string id) 
    { 
     _number.Add(id); 
    } 
} 

有了這個修改後的課,你不必讓您的測試方法進行任何更改。