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

时间:2014-11-13 10:31:14

标签: unit-testing testing mocking automated-tests moq

我有这个方法:

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
}

我想进行单元测试,所以我会模拟(使用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)
}

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

  

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

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

评论后编辑

这是异常的堆栈跟踪:

   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

1 个答案:

答案 0 :(得分:6)

在此Returns声明的Setup子句中:

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>());

看起来你不关心这个方法返回什么。但是你给Returns语句Func<ValidationResult.ValidationResult>。这是您的例外来源。

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

如果您想返回任何ValidationResult,请改为执行此操作:

.Returns(It.IsAny<Validazione.ValidationResult>())

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

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