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

时间:2015-11-08 21:51:36

标签: c# generics interface rhino-mocks invalidoperationexception

使用通用接口时,我遇到了似乎是Rhino Mocks的问题。为update方法创建存根时会抛出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.

1 个答案:

答案 0 :(得分:2)

很有趣,但我认为它希望你发送Arg代替&#34; 1&#34;

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