0
我遇到了使用通用接口時似乎是Rhino Mocks問題。當產生用於更新方法的引發InvalidOperationExceptionRhino Mocks存根與通用接口導致InvalidOperationException
接口
public interface IDB<T>
{
String add(T obj);
void update(String id, T obj);
}
工作單位存根測試
[Test Method]
public void AddWorks()
{
...
var stubRepo = MockRepository.GenerateStub<IDB<Location>>();
stubRepo.Stub(x => x.Add(Arg<Location>.Is.Anything)).Return("2342");
...
}
失敗的單元測試
[Test Method]
public void UpdateFails()
{
Location locObj = null;
...
var stubRepo = MockRepository.GenerateStub<IDB<Location>>();
stubRepo.Stub(x => x.Update("1", Arg<Location>.Is.Anything))
.WhenCalled((x) =>
{
locObj = (Location)x.Arguments[1];
});
...
}
堆棧跟蹤:
at Rhino.Mocks.ArgManager.CheckMethodSignature(MethodInfo method)
at Rhino.Mocks.Impl.RecordMockState.BuildParamExpectation(IInvocation invocation, MethodInfo method)
at Rhino.Mocks.Impl.RecordMockState.MethodCall(IInvocation invocation, MethodInfo method, Object[] args)
at Rhino.Mocks.MockRepository.MethodCall(IInvocation invocation, Object proxy, MethodInfo method, Object[] args)
at Rhino.Mocks.Impl.Invocation.Actions.RegularInvocation.PerformAgainst(IInvocation invocation)
at Rhino.Mocks.Impl.RhinoInterceptor.Intercept(IInvocation invocation)
at Castle.DynamicProxy.AbstractInvocation.Proceed()
at Castle.Proxies.IGenericDB`1Proxyf8be80cd084d43de8371e6582e3b79eb.IGenericDB`1.Update(String id, Location obj)
at APIUnitTestS.LocationModuleUnitTests.<>c.<Put_with_valid_json_should_return_ok>b__6_4(IGenericDB`1 x) in C:\Users\nwmot_000\Documents\code\Experiments\ASP.Net Gear Tracker\RPG Gear Tracker\APIUnitTestS\LocationModuleUnitTests.cs:line 250
at Rhino.Mocks.RhinoMocksExtensions.GetExpectationsToVerify[T](T mock, Action`1 action, Action`1 setupConstraints)
at Rhino.Mocks.RhinoMocksExtensions.AssertWasCalled[T](T mock, Action`1 action, Action`1 setupConstraints)
at Rhino.Mocks.RhinoMocksExtensions.AssertWasCalled[T](T mock, Action`1 action)
at APIUnitTestS.LocationModuleUnitTests.Put_with_valid_json_should_return_ok() in C:\Users\nwmot_000\Documents\code\Experiments\ASP.Net Gear Tracker\RPG Gear Tracker\APIUnitTestS\LocationModuleUnitTests.cs:line 250
Result Message:
Test method APIUnitTestS.LocationModuleUnitTests.Put_with_valid_json_should_return_ok threw exception:
System.InvalidOperationException: When using Arg<T>, all arguments must be defined using Arg<T>.Is, Arg<T>.Text, Arg<T>.List, Arg<T>.Ref or Arg<T>.Out. 2 arguments expected, 1 have been defined.
**臉手掌**感謝沒收。 –