我是模拟单元测试的新手。
我有这个方法:
virtual public bool Authenticate(
Identity identity,
out IdentityReference entityReference,
out RightAssignment rights)
{
entityReference = null;
rights = null;
string passwordHash = identity.Passwd;
string customerId;
string userId;
using (IScope scope = DataAccess.GetScope())
{
DA.eCheckResult result = DataAccess.Check(
scope,
identity.Domain,
identity.Login,
passwordHash,
identity.BranchIdentification,
identity.CustomerIdentification,
out customerId,
out userId);
if (result == eCheckResult.cOK)
{
entityReference = new IdentityReference
{
CustomerId = customerId,
UserId = userId
};
rights = DataAccess.GetRightAssignment(scope, userId);
}
return DA.eCheckResult.cOK == result;
}
}
DataAccess 是这样声明的接口:
public interface IAuthenticateDA : IDisposable
{
eCheckResult Check(
IScope scope,
string domain,
string login,
string passwordHash,
string branchIdentification,
string customerIdentification,
out string customerId,
out string userId);
RightAssignment GetRightAssignment(IScope scope, string userId);
/// <summary>
/// Gets the scope of the underlying data accessor
/// </summary>
IScope GetScope();
}
我目前的模拟尝试如下所示:
MockRepository mocks = new MockRepository();
IAuthenticateDA mockAuthenticateDA = mocks.StrictMock<IAuthenticateDA>();
string customerId;
string userId;
Expect.Call(mockAuthenticateDA.GetScope()).Return(null);
Expect.Call(mockAuthenticateDA.Check(
null,
"orgadata",
"test",
"test",
"branch",
"customer",
out customerId,
out userId)).Return(eCheckResult.cOK);
Expect.Call(mockAuthenticateDA.GetRightAssignment(null, "userId"))
.Return(MockupDA.Rights);
这是我的错误信息:
Die Orgadata.Auth.TestUnits.ServiceTest.TestBLAuthenticate-Testmethode hat eine Ausnahme ausgelöst: System.InvalidOperationException: Previous method 'IAuthenticateDA.GetScope();' requires a return value or an exception to throw.
Ergebnis StackTrace:
bei Rhino.Mocks.Impl.RecordMockState.AssertPreviousMethodIsClose()
bei Rhino.Mocks.Impl.RecordMockState.MethodCall(IInvocation invocation, MethodInfo method, Object[] args)
bei Rhino.Mocks.MockRepository.MethodCall(IInvocation invocation, Object proxy, MethodInfo method, Object[] args)
bei Rhino.Mocks.Impl.Invocation.Actions.RegularInvocation.PerformAgainst(IInvocation invocation)
bei Rhino.Mocks.Impl.RhinoInterceptor.Intercept(IInvocation invocation)
bei Castle.DynamicProxy.AbstractInvocation.Proceed()
bei Castle.Proxies.IAuthenticateDAProxy619e9a2b8594464d89e95c4149fb2ab3.IDisposable.Dispose()
bei Microsoft.Practices.Unity.ContainerControlledLifetimeManager.Dispose(Boolean disposing) in c:\tfs\EL\V5-SL\UnityTemp\Compile\Unity\Unity\Src\Lifetime\ContainerControlledLifetimeManager.cs:Zeile 76.
bei Microsoft.Practices.Unity.ContainerControlledLifetimeManager.Dispose() in c:\tfs\EL\V5-SL\UnityTemp\Compile\Unity\Unity\Src\Lifetime\ContainerControlledLifetimeManager.cs:Zeile 60.
bei Microsoft.Practices.ObjectBuilder2.LifetimeContainer.Dispose(Boolean disposing) in c:\tfs\EL\V5-SL\UnityTemp\Compile\Unity\Unity\Src\ObjectBuilder\Lifetime\LifetimeContainer.cs:Zeile 103.
bei Microsoft.Practices.ObjectBuilder2.LifetimeContainer.Dispose() in c:\tfs\EL\V5-SL\UnityTemp\Compile\Unity\Unity\Src\ObjectBuilder\Lifetime\LifetimeContainer.cs:Zeile 79.
bei Microsoft.Practices.Unity.UnityContainer.Dispose(Boolean disposing) in c:\tfs\EL\V5-SL\UnityTemp\Compile\Unity\Unity\Src\UnityContainer.cs:Zeile 466.
bei Microsoft.Practices.Unity.UnityContainer.Dispose() in c:\tfs\EL\V5-SL\UnityTemp\Compile\Unity\Unity\Src\UnityContainer.cs:Zeile 449.
bei Orgadata.Framework.IoC.Unity.TransientContainerProvider.Dispose(Boolean disposing) in x:\sourcen\Services\Framework\Orgadata.Framework.IoC.Unity\TransientContainerProvider.cs:Zeile 101.
bei Orgadata.Framework.IoC.Unity.TransientContainerProvider.Dispose() in x:\sourcen\Services\Framework\Orgadata.Framework.IoC.Unity\TransientContainerProvider.cs:Zeile 111.
bei Orgadata.Auth.TestUnits.ServiceTest.TestBLAuthenticate() in x:\sourcen\Services\Authentification\Orgadata.Auth.TestUnits\ServiceTest.cs:Zeile 168.