gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-28 19:55:47 24 4
gpt4 key购买 nike

我有这个方法:

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

它构建了,听起来我很正确,但最后我得到:

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

我谷歌了一下,但我不明白原因。对我来说还可以。

评论后编辑

这是异常的堆栈跟踪:

   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

最佳答案

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 */ )

关于unit-testing - Mocked 方法调用中的参数计数不匹配,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26906718/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com