在模拟的方法调用参数数量不匹配 [英] Parameter count mismatch in a Mocked method call

查看:307
本文介绍了在模拟的方法调用参数数量不匹配的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有这样的方法:

 公共无效Valida(Validazione.IValidator<&MyType的GT;验证)
{
    // 做一点事...
    Validazione.IMapper<&MyType的GT;映射器=新MyTypeMapper();
    为ValidationResult结果= validator.Validate(myTypeObj,映射器,新ValidationConfiguration());
    // ...继续做别的事情
}

这是我想要的单元测试,所以我会嘲笑(使用起订量架构)验证来引导验证的结果方法,所以我写了这个单元测试:

  [TestMethod的]
公共无效Long_test_name_as_best_practice()
{
    //安排
    MyAggregateRoot aggregateRoot = AggregateRoot.Stub();
    VAR mockedValidator =新的模拟< Validazione.IValidator<&MyType的GT;>();    mockedValidator.Setup(一个= GT; a.Validate(
        It.Is<&MyType的GT;(X => x.Id == Guid.Parse(3F2504E0-4F89-11D3-9A0C-0305E82C3301)),
        It.IsAny< Validazione.IMapper<&MyType的GT;>()
        It.IsAny< ValidationConfiguration>()
    ))返回< Validazione.ValidationResult>(X => X = It.IsAny< Validazione.ValidationResult>());    //行为
    aggregateRoot.Valida(mockedValidator.Object);    //断言(现在表现出对可读性的缘故)
}

它建立,它的声音我pretty正确的,但最终我得到:


  

类型的异常System.Reflection.TargetParameterCountException
  mscorlib.dll中发生,但在用户code没有处理
  其他信息:参数数量不匹配


我周围的谷歌,但我不明白其中的道理。对我来说似乎是好了。

评论(县)后修改

这是异常的堆栈跟踪:

 在System.Reflection.RuntimeMethodInfo.InvokeArgumentsCheck(obj对象,的BindingFlags invokeAttr,粘结剂粘合,对象[]参数,CultureInfo的文化)
   在System.Reflection.RuntimeMethodInfo.UnsafeInvoke(obj对象,的BindingFlags invokeAttr,粘结剂粘合,对象[]参数,CultureInfo的文化)
   在System.Delegate.DynamicInvokeImpl(对象[]参数)
   在System.Delegate.DynamicInvoke(对象[]参数)
   在Moq.Extensions.Invoke preserveStack(代表德尔,对象[]参数)
   在Moq.MethodCallReturn`2.Execute(ICallContext调用)
   在Moq.ExecuteCall.HandleIntercept(ICallContext调用,InterceptorContext CTX,CurrentInterceptContext localctx)
   在Moq.Interceptor.Intercept(ICallContext调用)
   在Moq.Proxy.CastleProxyFactory.Interceptor.Intercept(IInvocation调用)
   在Castle.DynamicProxy.AbstractInvocation.Proceed()
   在Castle.Proxies.IValidator`1Proxy.Validate(MyType的myTypeObj,IMapper`1映射器,ValidationConfiguration配置)
   在MyNamespace.Valida(IValidator`1验证)在C:\\ SVILUPPO \\项目名\\ MAIN \\ SRC \\ Project.MySubProject.Domain \\ filename.cs:里加104
   在MyTestNamespace.Long_test_name_as_best_practice()在C:\\ SVILUPPO \\项目名\\ MAIN \\ SRC \\ Project.SubProject.Domain.Tests \\ Test_AggregateCommand.cs:里加103


解决方案

返回的这一条款设置声明:

  mockedValidator.Setup(A => a.Validate(
    It.Is<&MyType的GT;(X => x.Id == Guid.Parse(3F2504E0-4F89-11D3-9A0C-0305E82C3301)),
    It.IsAny< Validazione.IMapper<&MyType的GT;>()
    It.IsAny< ValidationConfiguration>()
))返回< Validazione.ValidationResult>(X => X = It.IsAny< Validazione.ValidationResult>());

它看起来像你不在乎这个什么方法返回。但你给返回语句 Func键< ValidationResult.ValidationResult> 。这是你的异常从哪里来。

本语法是为了让您可以根据输入的方法是设置(这里的验证),所以你应该来计算一个返回值通过在函数功能使用相同的参数作为方法是设置。因为你的方法有3个参数,你给返回这需要1的函数,有一个参数不匹配。

如果你只想返回任何为ValidationResult ,而是执行此操作:

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

如果你实际上要计算一个为ValidationResult 根据参数验证,做到这一点:

  .Returns< MyType的,Validazione.IMapper<的MyType>中ValidationConfiguration>
    ((M,T,C)=> / * TODO:计算为ValidationResult * /)

I have this method:

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
}

that I want to unit test, so I would mock (using Moq framework) validator to steer the result of Validate method, so I wrote this unit test:

[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)
}

It builds, it sounds me pretty correct but in the end I get:

An exception of type 'System.Reflection.TargetParameterCountException' occurred in mscorlib.dll but was not handled in user code Additional information: Parameter count mismatch

I google around but I can't understand the reason. To me seems Ok.

Edit after comment(s)

This is the stack trace of the exception:

   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

解决方案

In the Returns clause of this Setup statement:

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

It looks like you don't care what this method returns. But you're giving the Returns statement a Func<ValidationResult.ValidationResult>. This is where your exception is coming from.

This syntax is intended to allow you to compute a return value based on the inputs to the method being setup (here it's Validate), and so you're supposed to pass in a Func with the same arguments as the method being setup. Since your method has 3 arguments and you give Returns a function that takes 1, there's an argument mismatch.

If you want to just return any ValidationResult, do this instead:

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

If you actually do want to compute a ValidationResult based on arguments to Validate, do this:

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

这篇关于在模拟的方法调用参数数量不匹配的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆