C# 在c中将字符串列表转换为小写的单元测试失败

C# 在c中将字符串列表转换为小写的单元测试失败,c#,string,list,unit-testing,C#,String,List,Unit Testing,我的单元测试在c语言中一直失败,我尝试了几种不同的方法。任何帮助都将不胜感激。它只是没有把我添加的书转换成小写。所以测试失败了 private List<string> _number; public Book (string[] id) { //_number = idents.Select (d => d.ToLower ()).ToList (); _number = new List<string>

我的单元测试在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"));
    }

testmethod不应该是这样的吗:

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

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

至少我的psycic crystal ball感觉到了这一点。

解决这个问题的更好方法不是将键转换成小写,而是使用一种可以以不区分大小写的方式存储键的构造。这将节省处理时间并减少编程错误

如果您只对存储图书密钥感兴趣,那么我强烈建议您使用

名单上写着,而另一个则写着。如果您有很多条目,这是一个显著的差异

下面是使用哈希集重写Book类的过程:

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);
    }
}

使用此修订的类,您不必对测试方法进行任何更改。

您是否将其转换为AddIdentifier中的小写字母?不确定为什么会被否决,似乎是一个合理的问题。你的名字变量在哪里?你的方法声明了吗?你很可能被否决了,因为你让我们在我们的心理水晶球中查找你的错误信息。你知道不是每个人都有这么酷的调试工具。你的代码会编译,测试也会通过。或者你在其他地方有一个错误阻止了它的编译,或者你没有发布准确的代码