私はこのDistinctBy メソッドを拡張機能として使用していないという違いで使用しました。今、これを呼び出している別のメソッドの単体テストを書きたいので、戻り値を設定したいと思います。
「DistinctBy」メソッド
public IEnumerable<TSource> DistinctBy<TSource, TKey>(
IEnumerable<TSource> source, Func<TSource, TKey> keySelector)
{
HashSet<TKey> seenKeys = new HashSet<TKey>();
foreach (TSource element in source)
{
if (seenKeys.Add(keySelector(element)))
{
yield return element;
}
}
}
初期セットアップ 今のところ、私は次のようなものを持っています (私は Autofac の Moq、Automock 機能を使用しています):
List<Product> listProduct = new List<Product>{ product1, product2 };
mock.Mock<IHelpers>()
.Setup(r => r.DistinctBy<List<BeautyBoutiqueArticle>, int>(It.IsAny<List<BeautyBoutiqueArticle>>(), It.IsAny<Func<List<BeautyBoutiqueArticle>, int>>()))
.Returns(ieList)
.Verifiable();
しかし、それは機能していません。次のようなエラーが表示されます。
最適なオーバーロードされたメソッドの一致.... に無効な引数が含まれているか、引数 1: 'System.Collections.Generic.List' から 'System.Collections.Generic.IEnumerable>' に変換できません。