Let's say that I have IService
interface:
public interface IService
{
string Name { get; set; }
}
And a delegate Func<IService>
that returns this interface.
In my unit test I want to mock the delegate's Invoke()
method using Moq like this:
[TestMethod]
public void UnitTest()
{
var mockService = new Mock<IService>();
var mockDelegate = new Mock<Func<IService>>();
mockDelegate.Setup(x => x.Invoke()).Returns(mockService.Object);
// The rest of the test
}
Unfortunately mockDelegate.Setup(...)
throws System.InvalidCastException
:
Test method UnitTest threw exception:
System.InvalidCastException: Unable to cast object of type 'System.Linq.Expressions.InstanceMethodCallExpressionN' to type 'System.Linq.Expressions.InvocationExpression'.
at Moq.ExpressionExtensions.GetCallInfo(LambdaExpression expression, Mock mock)
at Moq.Mock.<>c_DisplayClass1c`2.b_1b()
at Moq.PexProtector.Invoke(Func`1 function)
at Moq.Mock.Setup(Mock
1 mock, Expression
1 expression, Condition condition)at Moq.Mock
1.Setup(Expression
1 expression)at UnitTest() in UnitTests.cs: line 38
Line 38 is mockDelegate.Setup(x => x.Invoke()).Returns(mockService.Object);
Am I missing something? Or mocking delegate invocation is generally not a good idea?
Thank you.