I have following code.
public class MyClass
{
protected virtual IEnumerable<TResult> MyMethod<TResult>(Func<IDataReader, IEnumerable<TResult>> arg)
{
....
}
}
How can I Setup a Mock method for this?
I tried following but getting an error.
using Moq;
using Moq.Protected;
namespace Foo
{
[TestClass]
public class TestClass
{
Mock<MyClass> m_mockObject = null;
[TestMethod]
public void MyTest()
{
m_mockObject = new Mock<MyClass>();
AddMethod<Func<IDataReader, IEnumerable<SomeOtherClass>>, IEnumerable<SomeOtherClass>>(this.MyMethod);
}
private void AddMethod<TIn, TResult>(Func<TIn, TResult> method)
{
m_mockObject.Protected().Setup<TResult>(method.Method.Name, ItExpr.IsAny<TIn>())
.Returns(method); /* THIS LINE IS THROWING THE EXCEPTION */
}
public IEnumerable<TResult> MyMethod<TResult>(Func<IDataReader, IEnumerable<TResult>> arg)
{
....
}
}
}
Once I run the code I get following error on the call to Setup function.
> System.ArgumentException was unhandled by user code HResult=-2147024809 Message=Expression of type 'System.Object' cannot be used for return type 'System.Collections.Generic.IEnumerable`1[SomeOtherClass]' Source=System.Core StackTrace: at System.Linq.Expressions.Expression.ValidateLambdaArgs(Type delegateType, Expression& body, ReadOnlyCollection`1 parameters) at System.Linq.Expressions.Expression.Lambda[TDelegate](Expression body, String name, Boolean tailCall, IEnumerable`1 parameters) at System.Linq.Expressions.Expression.Lambda[TDelegate](Expression body, Boolean tailCall, IEnumerable`1 parameters) at System.Linq.Expressions.Expression.Lambda[TDelegate](Expression body, ParameterExpression[] parameters) at Moq.Protected.ProtectedMock`1.Setup[TResult](String methodOrPropertyName, Object[] args) .... ..... InnerException: