我有一个类,我想在我的测试中模拟,这是它界面的一部分:
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<>
method,而是GetAllInstrumentsAffectedByFixSide
.
为什么 Rhino 试图在 mock-call for中调用GetGenericMethodDefinition
for 方法?电话发生在以前。看起来它只是混淆了这两种方法。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 的直接设置时,它实际上可以工作。有什么想法吗?我已经更改了上面的代码,以反映问题。