I'm trying to mock an abstract class from a library that I'm using. I don't have access to the source code, only the decompiled version:
public abstract class Event : IEnumerable<Message>, IEnumerable
{
protected Event();
public abstract bool IsValid { get; }
public IEnumerator<Message> GetEnumerator();
public IEnumerable<Message> GetMessages();
}
This decompiled code confuses me slighty. First, the redundant inheritance, and also there's no implementation of non-abstract methods e.g. GetEnumerator
or IEnumerable.GetEnumerator()
. But it has compiled, and it works so I suppose it's just an artefact of the decompilation (if that is even a thing?)
I have tried the following mock, which compiles and runs without throwing exceptions.
public static Event GetMockedEvent()
{
var mock = new Mock<Event>();
mock.Setup(e => e.IsValid).Returns(true);
mock.As<IEnumerable>().Setup(e => e.GetEnumerator()).Returns(MessageList());
return mock.Object;
}
private static IEnumerator<Message> MessageList()
{
yield return GetMockedMessage();
yield return GetMockedMessage();
}
private static Message GetMockedMessage()
{
var mock = new Mock<Message>();
// Unimportant setups...
return mock.Object;
}
However I don't get any elements in the mocked object which I test in the following way
var ev = GetMockedEvent();
foreach (var msg in ev)
{
//
}
But the enumeration is empty, and I cannot figure out why. I have scratched my head with this issue for a full day now so I'd be very appreciative of your help.
Kind regards