Mocked方法调用中的参数计数不匹配

BAD*_*EED 3 testing automated-tests unit-testing moq mocking

我有这个方法:

public void Valida(Validazione.IValidator<MyType> validator)
{
    // do something...
    Validazione.IMapper<MyType> mapper = new MyTypeMapper();
    ValidationResult result = validator.Validate(myTypeObj, mapper, new ValidationConfiguration());
    // ...continue doing something else
}
Run Code Online (Sandbox Code Playgroud)

我想进行单元测试,所以我会模拟(使用Moq框架)validator来控制Validate方法的结果,所以我写了这个单元测试:

[TestMethod]
public void Long_test_name_as_best_practice()
{
    // arrange
    MyAggregateRoot aggregateRoot = AggregateRoot.Stub();
    var mockedValidator = new Mock<Validazione.IValidator<MyType>>();

    mockedValidator.Setup(a => a.Validate(
        It.Is<MyType>(x => x.Id == Guid.Parse("3F2504E0-4F89-11D3-9A0C-0305E82C3301")),
        It.IsAny<Validazione.IMapper<MyType>>(), 
        It.IsAny<ValidationConfiguration>()
    )).Returns<Validazione.ValidationResult>(x => x = It.IsAny<Validazione.ValidationResult>());

    // act
    aggregateRoot.Valida(mockedValidator.Object);

    // Assert (now showed for readability sake)
}
Run Code Online (Sandbox Code Playgroud)

它构建,听起来我非常正确,但最终我得到:

mscorlib.dll中出现"System.Reflection.TargetParameterCountException"类型的异常但未在用户代码中处理附加信息:参数计数不匹配

我谷歌周围,但我无法理解原因.对我来说似乎很好.

评论后编辑

这是异常的堆栈跟踪:

   in System.Reflection.RuntimeMethodInfo.InvokeArgumentsCheck(Object obj, BindingFlags invokeAttr, Binder binder, Object[] parameters, CultureInfo culture)
   in System.Reflection.RuntimeMethodInfo.UnsafeInvoke(Object obj, BindingFlags invokeAttr, Binder binder, Object[] parameters, CultureInfo culture)
   in System.Delegate.DynamicInvokeImpl(Object[] args)
   in System.Delegate.DynamicInvoke(Object[] args)
   in Moq.Extensions.InvokePreserveStack(Delegate del, Object[] args)
   in Moq.MethodCallReturn`2.Execute(ICallContext call)
   in Moq.ExecuteCall.HandleIntercept(ICallContext invocation, InterceptorContext ctx, CurrentInterceptContext localctx)
   in Moq.Interceptor.Intercept(ICallContext invocation)
   in Moq.Proxy.CastleProxyFactory.Interceptor.Intercept(IInvocation invocation)
   in Castle.DynamicProxy.AbstractInvocation.Proceed()
   in Castle.Proxies.IValidator`1Proxy.Validate(MyType myTypeObj, IMapper`1 mapper, ValidationConfiguration configuration)
   in MyNamespace.Valida(IValidator`1 validator) in c:\Sviluppo\ProjectName\Main\src\Project.MySubProject.Domain\filename.cs:riga 104
   in MyTestNamespace.Long_test_name_as_best_practice() in c:\Sviluppo\ProjectName\Main\src\Project.SubProject.Domain.Tests\Test_AggregateCommand.cs:riga 103
Run Code Online (Sandbox Code Playgroud)

Pat*_*irk 7

ReturnsSetup声明的条款中:

mockedValidator.Setup(a => a.Validate(
    It.Is<MyType>(x => x.Id == Guid.Parse("3F2504E0-4F89-11D3-9A0C-0305E82C3301")),
    It.IsAny<Validazione.IMapper<MyType>>(), 
    It.IsAny<ValidationConfiguration>()
)).Returns<Validazione.ValidationResult>(x => x = It.IsAny<Validazione.ValidationResult>());
Run Code Online (Sandbox Code Playgroud)

看起来你不关心这个方法返回什么.但你要发表Returns声明了Func<ValidationResult.ValidationResult>.这是您的例外来源.

此语法旨在允许您根据正在设置的方法(此处为Validate)的输入计算返回值,因此您应该使用Func与正在设置的方法相同的参数传递.由于您的方法有3个参数,并且您给出Returns的函数为1,因此参数不匹配.

如果您想要返回任何内容ValidationResult,请执行以下操作:

.Returns(It.IsAny<Validazione.ValidationResult>())
Run Code Online (Sandbox Code Playgroud)

如果您确实想要ValidationResult根据参数计算Validate,请执行以下操作:

.Returns<MyType, Validazione.IMapper<MyType>, ValidationConfiguration>
    ((m,t,c) => /* TODO: compute a ValidationResult */ )
Run Code Online (Sandbox Code Playgroud)