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

Rhino Mocks Stub with a generic interface causes InvalidOperationException

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

堆栈跟踪:

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.

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

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