我正在使用 spring4d、dunitx 和 delphi-mocks 编写我的第一个单元测试。(spring4d 发布 1.1 - 12.09.2014)
在我的测试应用程序中,我将一个接口自动注入到我的被测系统类(sut):
IMyInterface = interface [{yes, a GUID here}]
function GetSomething1: TSomething1;
function GetSomething2: TSomething2;
end;
TMyClass = class
private
[Inject]
FMyInterface: IMyInterface;
// ...
end;
现在,当我使用带有模拟的单元测试时,我使用以下(非常简单的)代码:
TMyTest = class
private
[Test]
procedure Test1;
[Test]
procedure Test2;
// ...
end;
procedure TMyTest.Test1;
var
aSut: TMyClass;
aIntfMock: TMock<IMyInterface>;
aSomething1: TSomething1;
begin
// Arrange
GlobalContainer.RegisterType<TMyClass>;
aIntfMock := TMock<IMyInterface>.Create;
try
GlobalContainer.RegisterType<IMyInterface>.DelegateTo(
function: IMyInterface
begin
Result := aIntfMock;
end)
.AsDefault;
GlobalContainer.Build;
aSomething1 := TSomething1.Create;
try
aIntfMock.Setup.WillReturn(TValue.From<TSomething1>(aSomething1)).When.GetSomething1;
aSut := GlobalContainer.Resolve<TMyClass>;
try
// Act
aSut.DoSomething;
finally
FreeAndNil(aSut);
end;
// Assert
// ...
finally
FreeAndNil(aSomething1);
end;
finally
aIntfMock.Free;
end;
end;
procedure TMyTest.Test2;
var
aSut: TMyClass;
aIntfMock: TMock<IMyInterface>;
aSomething2: TSomething2;
begin
// Arrange
GlobalContainer.RegisterType<TMyClass>;
aIntfMock := TMock<IMyInterface>.Create;
try
GlobalContainer.RegisterType<IMyInterface>.DelegateTo(
function: IMyInterface
begin
Result := aIntfMock;
end)
.AsDefault;
GlobalContainer.Build; // <---- ERegistrationException here
aSomething2 := TSomething1.Create;
try
aIntfMock.Setup.WillReturn(TValue.From<TSomething2>(aSomething2)).When.GetSomething2;
aSut := GlobalContainer.Resolve<TMyClass>;
try
// Act
aSut.DoSomething;
finally
FreeAndNil(aSut);
end;
// Assert
// ...
finally
FreeAndNil(aSomething2);
end;
finally
aIntfMock.Free;
end;
end;
第一个测试方法(Test1)运行良好......但在第二个测试方法(Test2)中,在 GlobalContainer.Build spring4d 的行中引发异常:ERegistrationException('找到重复的服务名称:IMyInterface_u.IMyInterface@IMyInterface_u.IMyInterface' )。
是否有可能取消注册 aIntfMock,以便我可以为其他每个测试例程注册一个新的?
[编辑]所以解决方案是:
TMyTest = class
private
FTestContainer: TContainer;
FIntfMock: TMock<IMyInterface>;
FSut: TMyClass;
[Setup]
procedure Setup;
[TearDown]
procedure TearDown;
[Test]
procedure Test1;
[Test]
procedure Test2;
// ...
end;
//---------------------
procedure TMyTest.Setup;
begin
FTestContainer := TContainer.Create;
FTestContainer.RegisterType<TMyClass>;
FIntfMock := TMock<IMyInterface>.Create;
FTestContainer.RegisterType<IMyInterface>.DelegateTo(
function: IMyInterface
begin
Result := FIntfMock;
end)
.AsDefault;
FTestContainer.Build;
FSut := FTestContainer.Resolve<TMyClass>;
end;
procedure TMyTest.TearDown;
begin
FreeAndNil(FSut);
FIntfMock.Free;
FreeAndNil(FTestContainer);
end;
procedure TMyTest.Test1;
var
aSomething1: TSomething1;
begin
// Arrange
aSomething1 := TSomething1.Create;
try
aIntfMock.Setup.WillReturn(TValue.From<TSomething1>(aSomething1)).When.GetSomething1;
// Act
FSut.DoSomething;
// Assert
// ...
finally
FreeAndNil(aSomething1);
end;
end;
procedure TMyTest.Test2;
begin
// ...
end;
感谢您的快速答复...