我有一个课程,我想在我的测试中模拟,这是它的界面的一部分:
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;
}
所以这个方法实际上是在非泛型方法上调用的。当我在里面放置一个断点,并检查了这个方法信息是什么时,我发现它实际上不是IsRegistered<>
方法,而是GetAllInstrumentsAffectedByFixSide
。
为什么Rhino会尝试在GetGenericMethodDefinition
的模拟调用中为方法GetAllInstrumentsAffectedByFixSide
调用IsRegistered<>
?之前发生过GetGenericMethodDefinition
次电话。看起来它只是混淆了这两种方法。
stacktrace:
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
更新我在问题中犯了一个错误:我实际上将期望设置为:
inventory.Expect(x => x.IsRegistered<TestInstrument>("ActivatorInstrument", null)).IgnoreArguments().Return(true);
当我将其更改为直接设置而没有.Expect和LastCall时 - 它确实有效。有什么想法吗?我已经更改了上面的代码,以反映问题。
答案 0 :(得分:0)
版本3.6.1
已更正此问题我不相信从版本3.6.0更新应该是一个问题,因为所有构造仍然存在