2014-10-20 34 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); 
    } 
} 

有了这个修改后的课,你不必让您的测试方法进行任何更改。