gpt4 book ai didi

c# - 为什么 RhinoMocks 在我的测试中抛出 InvalidOperationException?

转载 作者:太空狗 更新时间:2023-10-30 01:11:11 26 4
gpt4 key购买 nike

这是我目前正在进行的测试:
(根据 Lees 的回答编辑)

[Test]
public void AddLockClassWithNullNameShouldCallInsertOnSessionWithEmptyString()
{
LockClass lockClass = new LockClass { Id = ValidId, Name = null };

using ( mockRepository.Record() ) {
sessionFactory.CreateSession();
LastCall.Return( session );

session.InsertWithId( lockClass );
LastCall.Return( lockClass );

session.Commit();
session.Dispose();
}

using ( mockRepository.Playback() ) {
controller = new LockClassPanelController( sessionFactory );
controller.AddLockClass( lockClass.Id, string.Empty );
}

mockRepository.VerifyAll();
}

运行测试结果:

Test 'Test.Unit.Controllers.LockClassPanelControllerTests.AddLockWithNullNameClassShouldCallInsertOnSessionWithEmptyString' failed:
System.InvalidOperationException : The operation is invalid because of the current object state. (translated from german, dunno if thats the original english wording)
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 ISessionProxy2762dfaac4274133bc97e10d4e5c35d0.InsertWithId[TEntity](TEntity entity)
Controllers\LockClassPanelController.cs(20,0): at Artea.Service.Controllers.LockClassPanelController.AddLockClass(Int32 id, String name)
Unit\Controllers\LockClassPanelControllerTests.cs(80,0): at Test.Unit.Controllers.LockClassPanelControllerTests.AddLockWithNullNameClassShouldCallInsertOnSessionWithEmptyString()

有什么想法吗?

编辑:
我刚刚发现,如果更改方法的第一行,它可以正常工作:

LockClass lockClass = new LockClass { Id = ValidId, Name = string.Empty };

(string.Empty 而不是 null)但是测试应该检查如果 Name 属性为 null 会发生什么,因此让 Name 为除 null 之外的任何其他内容都不会很有帮助.

编辑:
该代码实际上并没有测试我想测试的内容。该方法的第一行应该是

LockClass lockClass = new LockClass { Id = ValidId, Name = string.Empty };

那是我期望的对象。 LockClass 是一个 DTO,只有上面一行中初始化的两个属性和必需的 Equals 东西。
行为线应该是

controller.AddLockClass( lockClass.Id, null );

[SetUp] 创建所有模拟对象。我要测试的是,如果用户通过 GUI 手势(GUI 在 Controller 上调用 AddLockClass)创建一个名称为 null 的 LockClass 对象,则 Controller 会创建一个名称为空的对象。还有其他方法可以做到这一点,但现在必须是这样。更改后的代码确实有效(例如 Rhino 不会呕吐)。我仍然保留这个问题,因为很好奇为什么 Rhino 不喜欢原始代码。

使其完整:

private const int ValidId = 4711;
private const int InvalidId = 0;
private MockRepository mockRepository;
private ISessionFactory sessionFactory;
private ISession session;
private LockClassPanelController controller;

[SetUp]
public void Setup()
{
mockRepository = new MockRepository();
sessionFactory = mockRepository.StrictMock<ISessionFactory>();
session = mockRepository.StrictMock<ISession>();
}

编辑:

public void AddLockClass( int id, string name )
{
if ( id != 0 ) {
using ( var session = sessionFactory.CreateSession() ) {
session.InsertWithId( new LockClass { Id = id, Name = name } );
session.Commit();
}
LoadLockClasses();
view.Initialize();
}
}

最佳答案

Rhino Mocks 为您提供正确的异常。在您的 AddLockClass 中,您有以下行:

session.InsertWithId( new LockClass { Id = id, Name = ( name ?? string.Empty ) } );

这清楚地表明,如果您使用 null name 参数调用此方法,则在创建 LockClass 实例时它仍将使用空字符串。因此,正如您在问题的第一次编辑中注意到的那样,您在测试中应该期望的 LockClass 是:

LockClass lockClass = new LockClass { Id = ValidId, Name = string.Empty };

并且不是带有 null 的版本。如果您需要进一步说明,请告诉我。

关于c# - 为什么 RhinoMocks 在我的测试中抛出 InvalidOperationException?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3164559/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com