CollectionAssert 不能等于继承 ICollection 的 class
Cannot CollectionAssert equal a class which inherits ICollection
我有两个 class PCBDataHelper(下面的代码)对象,我想断言它们作为一组单元测试的一部分是相等的。但是,我无法通过测试。
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
}
当我使用 Assert.AreEqual(obj1, obj2)
时,我得到以下输出:
Assert.AreEqual failed. Expected:<...HADataFile+PCBDataHelper>. Actual:<....HADataFile+PCBDataHelper>.
这让我相信这是一个集合。但是,当 运行 为 CollectionAssert.AreEqual(obj1, obj2)
时,我得到以下预编译错误:
cannot convert from '...HADataFile.PCBDataHelper' to 'System.Collections.ICollection'
对于两个参数。
ICollection<T>
does not implement ICollection
, so if you want to use CollectionAssert.AreEqual
那么你的 class 也需要 ICollection
:
public class PCBDataHelper : ICollection<string>, ICollection
{
// snip - you need to implement the relevant parts here
}
我有两个 class PCBDataHelper(下面的代码)对象,我想断言它们作为一组单元测试的一部分是相等的。但是,我无法通过测试。
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
}
当我使用 Assert.AreEqual(obj1, obj2)
时,我得到以下输出:
Assert.AreEqual failed. Expected:<...HADataFile+PCBDataHelper>. Actual:<....HADataFile+PCBDataHelper>.
这让我相信这是一个集合。但是,当 运行 为 CollectionAssert.AreEqual(obj1, obj2)
时,我得到以下预编译错误:
cannot convert from '...HADataFile.PCBDataHelper' to 'System.Collections.ICollection'
对于两个参数。
ICollection<T>
does not implement ICollection
, so if you want to use CollectionAssert.AreEqual
那么你的 class 也需要 ICollection
:
public class PCBDataHelper : ICollection<string>, ICollection
{
// snip - you need to implement the relevant parts here
}