gpt4 book ai didi

c# - RhinoMocks 在泛型方法中抛出 InvalidOperationException

转载 作者:行者123 更新时间:2023-11-30 18:31:52 29 4
gpt4 key购买 nike

我有一个类,我想在我的测试中模拟它,这是它接口(interface)的一部分:

interface IInventory
{
Instrument[] GetAllInstrumentsAffectedByFixSide(int fixSideNumber);

bool IsRegistered<T>(string name, int? fixSideNumber) where T : InventoryObject;
}

我的记录是这样的:

using (mockRepository.Record())
{
inventory.GetAllInstrumentsAffectedByFixSide(0);
LastCall.Return(new Instrument[0]);

inventory.Expect(x => x.IsRegistered<TestInstrument>("ActivatorInstrument", null)).IgnoreArguments().Return(true)
}

但是当我在我的测试代码中写这个时:

TestHandler.Inventory.IsRegistered<TestInstrument>("ActivatorInstrument", null)

它抛出 InvalidOperationException。它抛出这个异常的地方很有趣——它是 MethodInfo.GetGenericMethodDefinition()

它的来源看起来像:

public override MethodInfo GetGenericMethodDefinition()
{
if (!IsGenericMethod)
throw new InvalidOperationException();
Contract.EndContractBlock();

return RuntimeType.GetMethodBase(m_declaringType, RuntimeMethodHandle.StripMethodInstantiation(this)) as MethodInfo;
}

所以这个方法实际上是在非泛型方法上调用的。当我在里面打断点,查看这个methodInfo是什么的时候,发现其实不是IsRegistered<>方法,但是 GetAllInstrumentsAffectedByFixSide .

为什么 Rhino 尝试调用 GetGenericMethodDefinition方法GetAllInstrumentsAffectedByFixSideIsRegistered<> 的模拟通话中? GetGenericMethodDefinition之前发生过电话。看起来它只是混淆了这两种方法。

堆栈跟踪:

at System.Reflection.RuntimeMethodInfo.GetGenericMethodDefinition()
at Rhino.Mocks.MethodRecorders.UnorderedMethodRecorder.MethodsEquals(MethodInfo method, ProxyMethodExpectationTriplet triplet)
at Rhino.Mocks.MethodRecorders.UnorderedMethodRecorder.GetAllExpectationsForProxyAndMethod(Object proxy, MethodInfo method)
at Rhino.Mocks.MethodRecorders.UnorderedMethodRecorder.CalcExpectedAndActual.Calculate(Object proxy, MethodInfo method, Object[] args)
at Rhino.Mocks.MethodRecorders.UnorderedMethodRecorder.CalcExpectedAndActual..ctor(UnorderedMethodRecorder parent, Object proxy, MethodInfo method, Object[] args)
at Rhino.Mocks.MethodRecorders.UnorderedMethodRecorder.UnexpectedMethodCall(IInvocation invocation, Object proxy, MethodInfo method, Object[] args)
at Rhino.Mocks.MethodRecorders.UnorderedMethodRecorder.DoGetRecordedExpectation(IInvocation invocation, Object proxy, MethodInfo method, Object[] args)
at Rhino.Mocks.MethodRecorders.MethodRecorderBase.GetRecordedExpectation(IInvocation invocation, Object proxy, MethodInfo method, Object[] args)
at Rhino.Mocks.Impl.ReplayMockState.DoMethodCall(IInvocation invocation, MethodInfo method, Object[] args)
at Rhino.Mocks.Impl.ReplayMockState.MethodCall(IInvocation invocation, MethodInfo method, Object[] args)
at Rhino.Mocks.MockRepository.MethodCall(IInvocation invocation, Object proxy, MethodInfo method, Object[] args)
at Rhino.Mocks.Impl.RhinoInterceptor.Intercept(IInvocation invocation)
at Castle.DynamicProxy.AbstractInvocation.Proceed()
at IInventoryProxy4a132be1cb07441cafba3f828d3ced66.IsRegistered[T](String name, Nullable`1 fixSideNumber)
at TestHandlerLibrary.DummyFixSideHandler.DoInitialization() in \RTX.Test.TestGear.DummyTestHandlerLibrary\DummyFixSideHandler.cs:line 87

Upd 我在问题中犯了一个错误:我实际上将期望设置为:

inventory.Expect(x => x.IsRegistered<TestInstrument>("ActivatorInstrument", null)).IgnoreArguments().Return(true);

当我将其更改为不使用 .Expect 并使用 LastCall 的直接设置时 - 它确实有效。有什么想法吗?我更改了上面的代码以反射(reflect)问题。

最佳答案

此问题已在版本 3.6.1 中得到纠正

我认为从版本 3.6.0 更新应该不是问题,因为所有构造都保留

关于c# - RhinoMocks 在泛型方法中抛出 InvalidOperationException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19452351/

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