我正在使用 spring4d、dunitx 和 delphi-mocks 编写我的第一个单元测试。(spring4d 版本 1.1 - 2014 年 9 月 12 日)
在我的测试应用程序中,我将一个接口自动连接到我的被测系统类 (sut):
IMyInterface = interface [{yes, a GUID here}]
function GetSomething1: TSomething1;
function GetSomething2: TSomething2;
end;
TMyClass = class
private
[Inject]
FMyInterface: IMyInterface;
// ...
end;
Run Code Online (Sandbox Code Playgroud)
现在,当我使用带有模拟的单元测试时,我使用以下(非常简单的)代码:
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; …Run Code Online (Sandbox Code Playgroud)