【发布时间】:2010-07-02 09:31:21
【问题描述】:
这是我目前正在进行的一项测试:
(根据 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 属性为空会发生什么,因此将Name 设置为除null 之外的其他任何东西都不会很有帮助。
编辑:
该代码实际上并没有测试我想要测试的内容。方法的第一行应该是
LockClass lockClass = new LockClass { Id = ValidId, Name = string.Empty };
那是我预期的对象。 LockClass 是一个 DTO,只有上面一行中初始化的两个属性和强制性的 Equals 东西。
行动线应该是
controller.AddLockClass( lockClass.Id, null );
[SetUp] 创建所有模拟对象。我要测试的是,如果用户通过 GUI 手势(GUI 在控制器上调用 AddLockClass)创建了一个 LockClass 对象,其中名称为空,则控制器创建一个名称为空的对象。还有其他方法可以做到这一点,但现在必须这样。更改后的代码确实有效(例如 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();
}
}
【问题讨论】:
-
controller.AddLockClass(lockClass.Id, string.Empty);
-
controller.AddLockClass() 的签名是什么?什么是会话工厂?什么是会话?它们是在哪里创建的?他们也是嘲笑吗?
-
除了 LockClassPanelController 之外的所有东西都被模拟了(毕竟这是一个 unit 测试)。
AddLockClass(int id, string name) -
sessionFactory 是一个创建会话(ISession、DB 访问会话、手动 DAL)的 ISessionFactory。不过没关系,反正都被嘲笑了。
-
真的还是抛出同样的异常吗?你能粘贴你的 AddLockClass 方法的代码吗?
标签: c# .net unit-testing tdd rhino-mocks