简体   繁体   中英

Cannot CollectionAssert equal a class which inherits ICollection

I have two objects of class PCBDataHelper (code below) which I would like to assert are equal as part of a suite of unit tests. However, I am unable to get the tests to pass.

public class PCBDataHelper : ICollection<string>
{
    //private List<string> pcbData = new List<string>();
    private readonly HashSet<string> pcbData = new HashSet<string>();
    private const int MaxItemLength = 16;
    private const int MaxNumberItems = 5;
    #region ICollection<string> Members

    public void Add(string item)
    {
        if (pcbData.Count == MaxNumberItems)
            throw new AIException("Max number of items reached. Cannot add item");

        if (string.IsNullOrWhiteSpace(item) || item.Length > MaxItemLength)
            throw new ArgumentException("PCBData item must not be null and have max length 16!");

        if(!pcbData.Add(item))
            throw new AIException("Cannot add item, it may already exist!");
    }

    public void Clear()
    {
        pcbData.Clear();
    }

    public bool Contains(string item)
    {
        return pcbData.Contains(item);
    }

    public void CopyTo(string[] array, int arrayIndex)
    {
        pcbData.CopyTo(array, arrayIndex);
    }

    public int Count
    {
        get { return pcbData.Count; }
    }

    public bool IsReadOnly
    {
        get { return false; }
    }

    public bool Remove(string item)
    {
        return pcbData.Remove(item);
    }

    #endregion

    #region IEnumerable<string> Members

    public IEnumerator<string> GetEnumerator()
    {
        return pcbData.GetEnumerator();
    }

    #endregion

    #region IEnumerable Members

    IEnumerator IEnumerable.GetEnumerator()
    {
        return pcbData.GetEnumerator();
    }

    #endregion
}

When I use Assert.AreEqual(obj1, obj2) I get the following output:

Assert.AreEqual failed. Expected:<...HADataFile+PCBDataHelper>. Actual:<....HADataFile+PCBDataHelper>.

Which leads me to believe it is a collection. However when run as CollectionAssert.AreEqual(obj1, obj2) I get the following pre-compile error:

cannot convert from '...HADataFile.PCBDataHelper' to 'System.Collections.ICollection'

for both arguments.

ICollection<T> does not implement ICollection , so if you want to use CollectionAssert.AreEqual then your class needs ICollection too:

public class PCBDataHelper : ICollection<string>, ICollection
{
    // snip - you need to implement the relevant parts here
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM