(根据李斯的回答编辑)
[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,因此
姓名
除了
无效的
不会很有帮助。
编辑:
LockClass LockClass=new LockClass{Id=ValidId,Name=string.Empty};
那是我期望的目标。
LockClass
是一个DTO,只有在上面的行中初始化的两个属性,并且
Equals
表演路线应该是
controller.AddLockClass( lockClass.Id, null );
[设置]创建所有模拟对象。我要测试的是,如果用户通过GUI手势(GUI调用
AddLockClass
要使其完整:
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();
}
}