Delphi 单元测试:为 CUT 编写一个简单的 spy
Delphi Unit Testing : Writing a simple spy for the CUT
我正在寻找一种轻松和简明的方法,为Delphi下的DUnitX测试框架编写间谍程序.
过去,我使用非常丑陋的方式来做到这一点:
[TestFixture]
Test = class(TObject)
public
[test]
procedure Test1;
end;
TMyClass = class(TObject)
protected
procedure MyProcedure; virtual;
end;
TMyTestClass = class(TMyClass)
protected
fMyProcedureCalled : Boolean;
procedure MyProcedure; override;
end
procedure TMyTestClass.MyProcedure;
begin
fMyProcedureCalled := true;
inherited;
end;
procedure Test.Test1;
var aObj : TMyTestClass;
begin
TMyTestClass.Create;
Assert.IsTrue(aObj.fMyProcedureCalled);
end;
所有这些代码都用于检查是否调用了过程。太啰嗦了!
有没有办法编写一个可以帮助我减少代码的间谍程序?
听起来像是 mock 的用例(我在这里使用 mock 这个词是因为大多数框架将它们的各种测试替身称为 mock)
在下面的示例中,我使用的是 DUnit,但它对 DUnitX 应该没有任何影响。我也在使用 Spring4D 1.2 的模拟功能(我没有检查 Delphi Mocks 是否支持这个)
unit MyClass;
interface
type
TMyClass = class
private
fCounter: Integer;
protected
procedure MyProcedure; virtual;
public
property Counter: Integer read fCounter;
end;
implementation
procedure TMyClass.MyProcedure;
begin
Inc(fCounter);
end;
end.
program Tests;
uses
TestFramework,
TestInsight.DUnit,
Spring.Mocking,
MyClass in 'MyClass.pas';
type
TMyClass = class(MyClass.TMyClass)
public
// just to make it accessible for the test
procedure MyProcedure; override;
end;
TMyTest = class(TTestCase)
published
procedure Test1;
end;
procedure TMyClass.MyProcedure;
begin
inherited;
end;
procedure TMyTest.Test1;
var
// the mock is getting auto initialized on its first use
// and defaults to TMockBehavior.Dynamic which means it lets all calls happen
m: Mock<TMyClass>;
o: TMyClass;
begin
// set this to true to actually call the "real" method
m.CallBase := True;
// do something with o
o := m;
o.MyProcedure;
// check if the expected call actually did happen
m.Received(Times.Once).MyProcedure;
// to prove that it actually did call the "real" method
CheckEquals(1, o.Counter);
end;
begin
RegisterTest(TMyTest.Suite);
RunRegisteredTests();
end.
请记住,这仅适用于虚拟方法。
我正在寻找一种轻松和简明的方法,为Delphi下的DUnitX测试框架编写间谍程序.
过去,我使用非常丑陋的方式来做到这一点:
[TestFixture]
Test = class(TObject)
public
[test]
procedure Test1;
end;
TMyClass = class(TObject)
protected
procedure MyProcedure; virtual;
end;
TMyTestClass = class(TMyClass)
protected
fMyProcedureCalled : Boolean;
procedure MyProcedure; override;
end
procedure TMyTestClass.MyProcedure;
begin
fMyProcedureCalled := true;
inherited;
end;
procedure Test.Test1;
var aObj : TMyTestClass;
begin
TMyTestClass.Create;
Assert.IsTrue(aObj.fMyProcedureCalled);
end;
所有这些代码都用于检查是否调用了过程。太啰嗦了!
有没有办法编写一个可以帮助我减少代码的间谍程序?
听起来像是 mock 的用例(我在这里使用 mock 这个词是因为大多数框架将它们的各种测试替身称为 mock)
在下面的示例中,我使用的是 DUnit,但它对 DUnitX 应该没有任何影响。我也在使用 Spring4D 1.2 的模拟功能(我没有检查 Delphi Mocks 是否支持这个)
unit MyClass;
interface
type
TMyClass = class
private
fCounter: Integer;
protected
procedure MyProcedure; virtual;
public
property Counter: Integer read fCounter;
end;
implementation
procedure TMyClass.MyProcedure;
begin
Inc(fCounter);
end;
end.
program Tests;
uses
TestFramework,
TestInsight.DUnit,
Spring.Mocking,
MyClass in 'MyClass.pas';
type
TMyClass = class(MyClass.TMyClass)
public
// just to make it accessible for the test
procedure MyProcedure; override;
end;
TMyTest = class(TTestCase)
published
procedure Test1;
end;
procedure TMyClass.MyProcedure;
begin
inherited;
end;
procedure TMyTest.Test1;
var
// the mock is getting auto initialized on its first use
// and defaults to TMockBehavior.Dynamic which means it lets all calls happen
m: Mock<TMyClass>;
o: TMyClass;
begin
// set this to true to actually call the "real" method
m.CallBase := True;
// do something with o
o := m;
o.MyProcedure;
// check if the expected call actually did happen
m.Received(Times.Once).MyProcedure;
// to prove that it actually did call the "real" method
CheckEquals(1, o.Counter);
end;
begin
RegisterTest(TMyTest.Suite);
RunRegisteredTests();
end.
请记住,这仅适用于虚拟方法。