我正在尝试在一个循环中设置一个模拟对象,该循环将为具有不同参数的函数调用返回不同的值:
var myMock= new Mock<IInterface>();
for (int i = 0; i < fromKeys.Count; ++i)
{
var value= new[] {new[] {1.0 + i}};
_values.Add(value);
myMock.Setup(x => x.Provide(fromKeys[i])).Returns(new Sth(fromKeys[i], value));
}
_myObject = myMock.Object;
但是当我用生产代码中的第一个键调用 Provide
时它崩溃了(而不是在测试设置期间):
System.Reflection.TargetInvocationException : Exception has been thrown by the target of an invocation.
----> System.ArgumentOutOfRangeException : Index was out of range. Must be non-negative and less than the size of the collection.
Parameter name: index
at System.RuntimeMethodHandle._InvokeMethodFast(IRuntimeMethodInfo method, Object target, Object[] arguments, ref SignatureStruct sig, MethodAttributes methodAttributes, RuntimeType typeOwner)
at System.RuntimeMethodHandle.InvokeMethodFast(IRuntimeMethodInfo method, Object target, Object[] arguments, Signature sig, MethodAttributes methodAttributes, RuntimeType typeOwner)
at System.Reflection.RuntimeMethodInfo.Invoke(Object obj, BindingFlags invokeAttr, Binder binder, Object[] parameters, CultureInfo culture, Boolean skipVisibilityChecks)
at System.Delegate.DynamicInvokeImpl(Object[] args)
at Moq.Evaluator.SubtreeEvaluator.Evaluate(Expression e)
at Moq.Matchers.LazyEvalMatcher.Matches(Object value)
at Moq.MethodCall.Matches(ICallContext call)
at System.Linq.Enumerable.LastOrDefault(IEnumerable`1 source, Func`2 predicate)
at Moq.ExtractProxyCall.HandleIntercept(ICallContext invocation, InterceptStrategyContext ctx)
at Moq.Interceptor.Intercept(ICallContext invocation)
at Castle.DynamicProxy.AbstractInvocation.Proceed()
at Castle.Proxies.IBrownianProviderProxy.Provide(BrowniansKey keys)
at MyCode.....
如何设置?
这是一个闭包问题。 x => x.Provide(fromKeys[i])
直到稍后才被评估。等到后面求值的时候,i
== fromKeys.Count
,已经越界了。如果这没有意义,我建议阅读更多关于闭包的内容。不过,简单的解决方案是添加一行,使您的代码看起来像这样(在 for
循环中)
for (int i = 0; i < fromKeys.Count; ++i)
{
int j = i;
myMock.Setup(x => x.Provide(fromKeys[j])).Returns(new Sth(fromKeys[j], _values[j]));
}
我是一名优秀的程序员,十分优秀!