我有,我想在我的測試,以嘲笑的一類,這裏是它的一部分的接口: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