我有,我想在我的测试,以嘲笑的一类,这里是它的一部分的接口:RhinoMocks在泛型方法抛出InvalidOperationException异常
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
。
为什么犀牛试图呼叫GetGenericMethodDefinition
方法GetAllInstrumentsAffectedByFixSide
模拟呼叫IsRegistered<>
? 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 - 它实际上工作。有什么想法吗?我改变了上面的代码,以反映问题。
什么版本的犀牛嘲笑的是你使用?我试图回顾历史,但我认为*这是/是与犀牛嘲弄有关的已知错误。你可以把这个问题的代码完整的例子放在一起,这样我们就可以自己复制它了吗? – vcsjones
3.6.0我可以尝试,但可能它不起作用 - 我有很多单元测试,并且有很多通用方法。出于某种原因,只有这会引发异常。我还没有发现任何差异 – Archeg
@vcsjones我对该帖子进行了更新,你可以看看吗? – Archeg