【发布时间】:2015-01-10 11:17:50
【问题描述】:
我有这个方法:
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
【问题讨论】:
-
异常的调用栈是什么?不清楚是你的测试代码还是你的生产代码有问题。
-
查看更新问题。 ;)
标签: unit-testing testing mocking automated-tests moq