この ReadOnlyCollection プロパティをモックしようとしています:
private readonly IList<MyClass> myList = new List<MyClass>();
public virtual ReadOnlyCollection<MyClass> MyList
{
get
{
return new ReadOnlyCollectionBuilder<MyClass>(this.myList).ToReadOnlyCollection();
}
}
このモックを使用して (ここで見られるように):
IList<MyClass> mockList = GetElements();
mockObj.SetupGet<IEnumerable<MyClass>>(o => o.myList).Returns(mockList);
ただし、実行時に InvalidCastException が発生します。
Unable to cast object of type 'System.Collections.Generic.List`1[MyClass]' to
type 'System.Collections.ObjectModel.ReadOnlyCollection`1[MyClass]'.
私は何を間違っていますか?