I am trying to mock a generic method, which has a constraint of T : IFoo
However moq cannot seem to understand the conversion and gives the following error:
The type 'Moq.It.IsSubtype<SomeNamepace.IFoo>' cannot be used as type parameter 'T' in the generic type or method 'IMyClass.DoSomething(Action)'. There is no implicit reference conversion from 'Moq.It.IsSubtype<SomeNamepace.IFoo>' to 'SomeNamepace.IFoo'.
public interface IFoo
{
}
class Foo : IFoo
{
}
public interface IMyClass
{
public IDisposable DoSomething<T>(Action<T> asd) where T : IFoo;
}
public class MyTest
{
[Test]
public void SomeTest()
{
var mock = new Mock<IMyClass>();
mock.Setup(e => e.DoSomething(It.IsAny<Action<IFoo>>())).Returns(Mock.Of<IDisposable>());
// What I want but gives compiler error
// mock.Setup(e => e.DoSomething(It.IsAny<Action<It.IsSubtype<IFoo>>>())).Returns(Mock.Of<IDisposable>());
// Action<IFoo> would work, but in real code its not used like that
Action<Foo> myAction = (e) => { };
var result = mock.Object.DoSomething(myAction);
result.Dispose(); // Null reference exception
}
}