CollectionAssert与泛型一起使用?

Nic*_*ner 33 .net c# generics unit-testing mstest

它似乎CollectionAssert不能与泛型一起使用.这太令人沮丧了; 我想测试的代码确实使用了泛型.我是什么做的?写样板文件在两者之间转换?手动检查集合等价?

这失败了:

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));
Run Code Online (Sandbox Code Playgroud)

编译器错误:

错误1:

'System.Collections.Generic.IEnumerator>'不包含'ToList'的定义,并且没有可以找到接受类型'System.Collections.Generic.IEnumerator>'的第一个参数的扩展方法'ToList'

错误2

'System.Collections.Generic.IEnumerator>'不包含'ToList'的定义,并且没有可以找到接受类型'System.Collections.Generic.IEnumerator>'的第一个参数的扩展方法'ToList'

错误3

'System.Collections.Generic.IEnumerator>'不包含'SequenceEquals'的定义,并且没有可以找到接受类型'System.Collections.Generic.IEnumerator>'的第一个参数的扩展方法'SequenceEquals'

我究竟做错了什么?我没有正确使用扩展程序吗?

更新:好的,这看起来好一点,但仍然不起作用:

IEnumerable<IDictionary<string, string>> expected = // ...

IEnumerable<IDictionary<string, string>> actual = // ...

CollectionAssert.AreEquivalent(expected.ToList(), actual.ToList()); // fails
CollectionAssert.IsSubsetOf(expected.ToList(), actual.ToList()); // fails
Run Code Online (Sandbox Code Playgroud)

我不想比较名单; 我只关心集合成员平等.成员的顺序并不重要.我怎么能绕过这个?

Mar*_*ann 35

可以将CollectionAssert与泛型集合一起使用.关键是要明白,CollectionAssert方法操作上CollectionAssert,虽然一些泛型集合接口来实现CollectionAssert,ICollection确实.

因此,您可以使用ICollection扩展方法来解决此限制:

IEnumerable<Foo> expected = //...
IEnumerable<Foo> actual = //...
CollectionAssert.AreEqual(expected.ToList(), actual.ToList());
Run Code Online (Sandbox Code Playgroud)

也就是说,我仍然认为CollectionAssert在许多其他方面都被破坏了,所以我倾向于使用Assert.IsTrue和LINQ扩展方法,如下所示:

Assert.IsTrue(expected.SequenceEqual(actual));
Run Code Online (Sandbox Code Playgroud)

FWIW,我目前正在使用这些扩展方法来执行其他比较:

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;
    }
}
Run Code Online (Sandbox Code Playgroud)


小智 5

如果您正在使用集合,那么请使用此成语

HashSet<string> set1  = new HashSet<string>(){"A","B"};
HashSet<string> set2  = new HashSet<string>(){"B","A"};

Assert.IsTrue(set1.SetEquals(set2));
Run Code Online (Sandbox Code Playgroud)


Joh*_*ers 2

您可以轻松编写自己的通用版本,然后将其移至所有测试中使用的基类或实用程序类。它基于 All 和 Any 等 LINQ 运算符。