We have a fairly complex interface hierarchy, and I'm struggling to get Moq to do what I want.
I have an IReservation interface that extends IRulesReservation and hides its enumerator with a new implementation of a different type.
public interface IReservation : IRulesReservation { new IEnumerator<IRoutePart> GetEnumerator(); }
IRulesReservation extends IEnumerable .
public interface IRulesReservation : IEnumerable<IRulesRoutePart> { }
The method I'm trying to execute accepts IReservation , but at different points you need to access IEnumerable<IRulesRoutePart> . My Mock is configured like this:
m_mock = new Mock<IReservation>(); m_mock.As<IRulesReservation>().Setup(r => r.GetEnumerator()).Returns(routeParts.Select(rp => (IRulesRoutePart)rp).GetEnumerator());
In this example, routeParts is a list of IRouteParts that comes from Mock<IRouteParts> objects that are configured using .As<IRulesRoutePart>() .
Whenever I get a little code in the function that I am testing, which uses an enumerator, it performs iteration on it, as if the collection were empty.
Am I doing something wrong in my setup? Or is Mock simply unable to process an enumerator that is hidden this way?
Edit: some weird behavior I just noticed when running test code in mock:
Assert.That((reservation.Object as IRulesReservation).Count() == 8); Assert.That((reservation.Object as IEnumerable<IRulesRoutePart>).Count() == 8);
The first line will pass, but the second line will fail. I tried changing the layout to specifically set the counter for IEnumerable<IRulesRoutePart> , but without effect:
m_mock.As<IEnumerable<IRulesRoutePart>>().Setup(r => r.GetEnumerator()).Returns(routeParts.Select(rp => (IRulesRoutePart)rp).GetEnumerator());