I'm running into a problem where I try to mock an object which contain a property Items
of type ICollection<>
. I get the following error :
System.NotSupportedException : Invalid setup on a non-virtual (overridable in VB) member: m => m.Items
The problem is that the property Items is already virtual.
I reproduced the error that I get in my project, in the Sandbox below :
public class ItemList
{
public virtual int Id { get; set; }
}
public class SpecialList
{
public virtual string Token { get; internal set; }
public virtual ICollection<ItemList> Items { get; private set; }
}
That error occurs in my Test when I try to SetupProperty
like that :
[TestFixture]
public class TestSpecialList
{
[Test]
public void Mocking_Virtual_ICollection()
{
var mockModel = new Mock<SpecialList>();
var listItem = new List<ItemList> {new ItemList {Id = 007}};
mockModel.SetupProperty(m => m.Items, listItem);
}
}
Am I doing something wrong? Is it possible to Moq an object containing a ICollection<> property?