无法验证是否调用了Moq'd Func

时间:2016-04-22 09:48:41

标签: c# unit-testing mocking moq

我正在尝试使用Moq模拟Func<Tin, Tout>并验证它是否被调用(调用)

我目前的代码是:

var handlers = new List<ICallBridgeHandler>();
var stationIdReader = new Mock<IStationIdReader>();
var loggerCreator = new Mock<Func<Type, ILogger>>();
loggerCreator.Setup(x => x.Invoke(typeof(Service))).Returns<ILogger>(null);

loggerCreator.Verify(x => x.Invoke(It.IsAny<Type>()), Times.Never);
var sut = new Service(stationIdReader.Object, handlers, loggerCreator.Object);
loggerCreator.Verify(x => x.Invoke(typeof(Service)), Times.Once);
Assert.IsNotNull(sut);

但这种情况正在爆发:

Result StackTrace:  
at Moq.ExpressionExtensions.GetCallInfo(LambdaExpression expression, Mock mock)
at Moq.Mock.<>c__DisplayClass1c`2.<Setup>b__1b()
at Moq.PexProtector.Invoke[T](Func`1 function)
at Moq.Mock.Setup[T,TResult](Mock`1 mock, Expression`1 expression, Condition condition)
at Moq.Mock`1.Setup[TResult](Expression`1 expression)
at Tests.ConstructorRetrievesLoggerFromCreator() in ...

Result Message: 
Test method Tests.ConstructorRetrievesLoggerFromCreator threw exception: 
System.InvalidCastException: Unable to cast object of type
'System.Linq.Expressions.InstanceMethodCallExpressionN' to type
'System.Linq.Expressions.InvocationExpression'.

任何人都可以帮忙解决这个问题,我需要能够检查构造函数是否调用了Func。

N.B。我试图删除设置步骤,因为我不关心此测试的返回值,我在验证调用上遇到了同样的问题。

显然我可以注入一个返回给定值的具体Func并以某种方式查询该值,但是我的Service类看起来像:

public class Service 
{
    private readonly ILogger _logger;

    [ImportingConstructor]
    public Service([Import("GetLogger")]Func<Type, ILogger> loggerCreator)
    {
        if (loggerCreator == null)
            throw new ArgumentNullException(nameof(loggerCreator));

        _logger = loggerCreator.Invoke(GetType());
    }
}

因此,在不更改我的类的情况下,我看不到成员变量_logger,因此我无法验证是否已调用loggerCreator,这就是我要验证的内容。

1 个答案:

答案 0 :(得分:1)

无需模拟该功能。创建一个Func变量并在其中保留一个计数器以跟踪。

[TestMethod]
public void Func_Should_Be_Invoked_Once() {
    //Arrange
    var handlers = new List<ICallBridgeHandler>();
    var stationIdReader = new Mock<IStationIdReader>();
    var mockLogger = new Mock<ILogger>();
    int loggerCreaterInvokeCalls = 0;
    Func<Type, ILogger> loggerCreator = (type) => {
        loggerCreaterInvokeCalls++;
        return mockLogger.Object;
    };
    //Act
    var sut = new Service(stationIdReader.Object, handlers, loggerCreator);
    //Assert
    Assert.IsNotNull(sut);
    Assert.AreEqual(1, loggerCreaterInvokeCalls);
}

参考:Using Moq to Mock a Func<> constructor parameter and Verify it was called twice