Hej Buddies!
I’m trying to create a mock for an interface like the one below:
public interface ITheInterface: IEnumerable
{
...
}
In these manners:
var theInterfaceMock = new Mock<ITheInterface>();
theInterfaceMock.Setup(x => x.OfType<SomeType>()).Returns(something);
and
var theInterfaceMock = new Mock<ITheInterface>();
theInterfaceMock.As<IEnumerable>();
theInterfaceMock.Setup(x => x.OfType<SomeType>()).Returns(something);
And in both cases I’m getting a System.NotSupportedException that basically tells me that that ITheInterface doesn’t have the OfType() method (when it actually does have it). Anybody knows any way to solve this issue?.
Thank you!
OfType is not a method on the IEnumerable, it’s an extension method called Enumberable.OfType.
This is why Moq is complaining I think. Since these are static classes, I think you’ll need to use a tool like typemock.
Question is however. Why you need to mock OfType()? You can trust that the microsoft implementation of Enumberable.OfType works. So just mock the the IEnumberable interface (GetEnumerator), and return a mock that support IEnumerator, that OfType will use.