带有通用接口的Rhino Mocks Stub会导致InvalidOperationException

本文关键字:Stub InvalidOperationException Mocks Rhino 接口 | 更新日期: 2023-09-27 18:21:03

我在使用通用接口时遇到了Rhino 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];
            });
    ...
}

StackTrace:

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.

带有通用接口的Rhino Mocks Stub会导致InvalidOperationException

很有趣,但我认为它希望您发送Arg而不是"1"

stubRepo.Stub(x => x.Update(Arg<string>.Is.Equal("1"), Arg<Location>.Is.Anything))