Not able to Mock Indexers in C# using SetupGet

Viewed 26

I have the following Interfaces with get properties and one indexer.

public interface IDummy
{
     public string Name{get;set;}
     public IDummyList DummyChildren{get;} 
}

public interface IDummyList
{
     IDummy this[string name]{get;}
}

Now I am trying to mock the IDummy like this

Mock<IDummy> mockDummy = new Mock<IDummy>{DefaultValue = DefaultValue.Mock}
Mock<IDummy> mockDummyChildren = new Mock<IDummy>{DefaultValue = DefaultValue.Mock}

mockDummy.SetupGet(x=>x.Name).Returns("XYZ");
mockDummyChildren.SetupGet(x=>x.DummyChildren["ABC"]).Return(mockDummy.Object)

But the above code doesn't set the mockdata for DummyChildren. I am trying to understand why it is not doing that. Since the properties are read only so I cannot set it directly. So need the Mock data to be used in the original function to test. Please let me know if there is any enhancement or I am missing out something here. Thanks.

0 Answers
Related