德尔福单元测试:为CUT编写一个简单的间谍

Lud*_*c C 2 delphi unit-testing dunit spy dunitx

我在寻找一种方式来轻松简洁地写为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;
Run Code Online (Sandbox Code Playgroud)

所有这些代码都用于检查是否调用了一个过程.那太冗长了!

有没有办法写一个可以帮助我减少代码的间谍?

Ste*_*nke 5

听起来像一个模拟的用例(我在这里使用术语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.
Run Code Online (Sandbox Code Playgroud)

请记住,这仅适用于虚拟方法.

  • 如果你需要从一个不受`$ RTTI`指令影响的基类测试一个方法,你可以使用一个小技巧并在`public`部分的子类中重新定义它作为`override; abstract;`这将导致生成RTTI. (2认同)