CollectionAssert泛型使用? [英] CollectionAssert use with generics?
问题描述
看来, CollectionAssert
不能与仿制药使用。这是超级沮丧;在code我想测试并使用泛型。我该怎么办?编写样板在两者之间转换?手动检查集合等价?
It appears that CollectionAssert
cannot be used with generics. This is super frustrating; the code I want to test does use generics. What am I to do? Write boilerplate to convert between the two? Manually check collection equivalence?
这将失败:
ICollection<IDictionary<string, string>> expected = // ...
IEnumerable<IDictionary<string, string>> actual = // ...
// error 1 and 2 here
CollectionAssert.AreEqual(expected.GetEnumerator().ToList(), actual.ToList());
// error 3 here
Assert.IsTrue(expected.GetEnumerator().SequenceEquals(actual));
编译器错误:
Compiler errors:
错误1:
System.Collections.Generic.IEnumerator>'不包含'了ToList',没有扩展方法'了ToList接受第一类型的参数'System.Collections.Generic.IEnumerator>的定义可以发现
'System.Collections.Generic.IEnumerator>' does not contain a definition for 'ToList' and no extension method 'ToList' accepting a first argument of type 'System.Collections.Generic.IEnumerator>' could be found
错误2
System.Collections.Generic.IEnumerator>'不包含'了ToList',没有扩展方法'了ToList接受第一类型的参数'System.Collections.Generic.IEnumerator>的定义可以发现
'System.Collections.Generic.IEnumerator>' does not contain a definition for 'ToList' and no extension method 'ToList' accepting a first argument of type 'System.Collections.Generic.IEnumerator>' could be found
错误3
System.Collections.Generic.IEnumerator>不包含对SequenceEquals的和定义没有扩展方法'SequenceEquals的接受第一类型的参数'System.Collections.Generic.IEnumerator>可以找到
'System.Collections.Generic.IEnumerator>' does not contain a definition for 'SequenceEquals' and no extension method 'SequenceEquals' accepting a first argument of type 'System.Collections.Generic.IEnumerator>' could be found
我是什么做错了吗?难道我不使用扩展是否正确?
What am I doing wrong? Am I not using extensions correctly?
更新:确定,这看起来有点好转,但仍然无法正常工作:
Update: Ok, this looks a bit better, but still doesn't work:
IEnumerable<IDictionary<string, string>> expected = // ...
IEnumerable<IDictionary<string, string>> actual = // ...
CollectionAssert.AreEquivalent(expected.ToList(), actual.ToList()); // fails
CollectionAssert.IsSubsetOf(expected.ToList(), actual.ToList()); // fails
我不希望比较列表;我只在乎组成员的平等。成员的顺序并不重要。我怎样才能解决这个?
I don't want to be comparing lists; I only care about set membership equality. The order of the members is unimportant. How can I get around this?
推荐答案
您可以使用CollectionAssert与泛型集合。关键是要明白,CollectionAssert方法对的ICollection
运行,尽管一些一般的集合接口落实的ICollection
,名单,其中,T&GT;
确实
You can use CollectionAssert with generic collections. The trick is to understand that the CollectionAssert methods operate on ICollection
, and although few generic collection interfaces implement ICollection
, List<T>
does.
因此,您可以使用了ToList
扩展方法绕过这个限制:
Thus, you can get around this limitation by using the ToList
extension method:
IEnumerable<Foo> expected = //...
IEnumerable<Foo> actual = //...
CollectionAssert.AreEqual(expected.ToList(), actual.ToList());
不过,我仍然认为CollectionAssert打破了很多其他的方式,所以我倾向于使用Assert.IsTrue与LINQ扩展方法,像这样的:
That said, I still consider CollectionAssert broken in a lot of other ways, so I tend to use Assert.IsTrue with the LINQ extension methods, like this:
Assert.IsTrue(expected.SequenceEquals(actual));
FWIW,我目前使用这些扩展方法来执行其他比较:
FWIW, I'm currently using these extension methods to perform other comparisons:
public static class EnumerableExtension
{
public static bool IsEquivalentTo(this IEnumerable first, IEnumerable second)
{
var secondList = second.Cast<object>().ToList();
foreach (var item in first)
{
var index = secondList.FindIndex(item.Equals);
if (index < 0)
{
return false;
}
secondList.RemoveAt(index);
}
return secondList.Count == 0;
}
public static bool IsSubsetOf(this IEnumerable first, IEnumerable second)
{
var secondList = second.Cast<object>().ToList();
foreach (var item in first)
{
var index = secondList.FindIndex(item.Equals);
if (index < 0)
{
return false;
}
secondList.RemoveAt(index);
}
return true;
}
}
这篇关于CollectionAssert泛型使用?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!