0

また、リストの比較専用のメソッドでアサーション ライブラリを使用すると、単体テストでこの 2 つの結果を一致させることができません。

var list1 = new List<Tuple<string, IEnumerable<string>>>
{
  Tuple.Create<string,IEnumerable<string>>("string", new[] { "value" })
};

var list2 = new List<Tuple<string, IEnumerable<string>>>
{
  Tuple.Create<string,IEnumerable<string>>("string", new[] { "value" })
};


var result = list1.All(a => list2.Any(a.Equals)); // result false

肯定的な一致を取得する唯一の方法は、タプルを分解Item2し、リストの比較をサポートする方法で一致させることです。

やらない方法ある?

4

1 に答える 1

3

You can use SequenceEqual extension method:

var result = list1.All(a => list2.Any(x => x.Item1 == a.Item1 && x.Item2.SequenceEqual(a.Item2)));

Returns true for your sample input.

The reason why you have to do it explicitly and it's not working by default when two Tuple<string, IEnumerable<string>> instances are compared is the second Tuple item. According to MSDN both components are checked agains each other to decide if Tuples are equal. Because the second one is an Array standard reference equality comparer is used. And because your Tuples are not pointing to the same Array in memory it returns false.

Stardard .Any(a.Equals) would work if only your Tuple objects were pointing to the same array:

var array = new[] { "value" };

var list1 = new List<Tuple<string, IEnumerable<string>>>
{
    Tuple.Create<string,IEnumerable<string>>("string", array)
};

var list2 = new List<Tuple<string, IEnumerable<string>>>
{
    Tuple.Create<string,IEnumerable<string>>("string", array)
};

var result = list1.All(a => list2.Any(a.Equals));

Returns true as well.

于 2013-03-28T06:45:47.017 に答える