원클릭으로
fpcunit-testing-patterns
Padrões de testes unitários com FPCUnit para FreePascal/Lazarus — fixtures, mocking, integration tests
Codex 또는 Claude로 설치 이 Prompt를 복사해 Codex, Claude 또는 다른 어시스턴트에 붙여 넣으면 Skill 페이지를 검토하고 설치를 진행할 수 있습니다.
메뉴
Padrões de testes unitários com FPCUnit para FreePascal/Lazarus — fixtures, mocking, integration tests
Codex 또는 Claude로 설치 이 Prompt를 복사해 Codex, Claude 또는 다른 어시스턴트에 붙여 넣으면 Skill 페이지를 검토하고 설치를 진행할 수 있습니다.
SOC 직업 분류 기준
Versao curta para migracao Delphi/IntraWeb -> Lazarus com equivalencia funcional. Foco em checklist objetivo, sem refatoracao e com checkpoint de progresso.
Migracao Delphi IntraWeb -> Lazarus IntraWeb com equivalencia funcional, sem refatorar regra de negocio. Inclui conversao FireDAC->Zeos, DFM->LFM, ajuste de units Delphi para FPC e fluxo com checkpoint (RETOMAR/STATUS/PAUSAR).
Padrões arquiteturais, de injeção de dependência e de UI para o ecossistema Projeto ACBr (Automação Comercial Brasil) em FreePascal/Lazarus.
Padrões pragmáticos de código limpo para FreePascal/Lazarus — conciso, direto, sem over-engineering
Checklist de revisão de código FreePascal/Lazarus — qualidade, segurança, performance, SOLID, memória
Implementação dos 23 padrões GoF (Gang of Four) em Object Pascal / FreePascal/Lazarus com interfaces, TInterfacedObject e princípios SOLID. Cobre Creational, Structural e Behavioral patterns.
| name | FPCUnit Testing Patterns |
| description | Padrões de testes unitários com FPCUnit para FreePascal/Lazarus — fixtures, mocking, integration tests |
Use esta skill ao criar ou organizar testes unitários em projetos FreePascal/Lazarus.
tests/
├── MeuApp.Tests.lpr ← Projeto de testes
├── Domain/
│ ├── MeuApp.Tests.Customer.Entity.pas
│ └── MeuApp.Tests.Order.Entity.pas
├── Application/
│ ├── MeuApp.Tests.Customer.Service.pas
│ └── MeuApp.Tests.Order.Service.pas
├── Infrastructure/
│ └── MeuApp.Tests.Customer.Repository.pas
└── Helpers/
├── MeuApp.Tests.Helpers.Database.pas
└── MeuApp.Tests.Helpers.Mocks.pas
unit MeuApp.Tests.Customer.Service;
interface
uses
FPCUnit.TestFramework,
MeuApp.Domain.Customer.Entity,
MeuApp.Domain.Customer.Repository.Intf,
MeuApp.Application.Customer.Service;
type
[TestFixture]
TCustomerServiceTest = class
private
FService: TCustomerService;
FMockRepo: ICustomerRepository;
public
[Setup]
procedure Setup;
[TearDown]
procedure TearDown;
[Test]
procedure CreateCustomer_WithValidData_ShouldSucceed;
[Test]
procedure CreateCustomer_WithEmptyName_ShouldRaiseException;
[Test]
procedure CreateCustomer_WithDuplicateCpf_ShouldRaiseException;
[Test]
procedure GetById_WithExistingId_ShouldReturnCustomer;
[Test]
procedure GetById_WithInvalidId_ShouldRaiseNotFoundException;
end;
implementation
uses
System.SysUtils;
{ TCustomerServiceTest }
procedure TCustomerServiceTest.Setup;
begin
FMockRepo := TMemoryCustomerRepository.Create;
FService := TCustomerService.Create(FMockRepo);
end;
procedure TCustomerServiceTest.TearDown;
begin
FService.Free;
// FMockRepo é interface — liberado automaticamente
end;
[Test]
procedure TCustomerServiceTest.CreateCustomer_WithValidData_ShouldSucceed;
begin
Assert.WillNotRaiseAny(
procedure
begin
FService.CreateCustomer('João Silva', '12345678901', 'joao@email.com');
end
);
end;
[Test]
procedure TCustomerServiceTest.CreateCustomer_WithEmptyName_ShouldRaiseException;
begin
Assert.WillRaise(
procedure
begin
FService.CreateCustomer('', '12345678901', 'joao@email.com');
end,
EValidationException
);
end;
[Test]
procedure TCustomerServiceTest.CreateCustomer_WithDuplicateCpf_ShouldRaiseException;
begin
FService.CreateCustomer('João', '12345678901', 'joao@email.com');
Assert.WillRaise(
procedure
begin
FService.CreateCustomer('Maria', '12345678901', 'maria@email.com');
end,
EBusinessRuleException
);
end;
[Test]
procedure TCustomerServiceTest.GetById_WithExistingId_ShouldReturnCustomer;
var
LCustomer: TCustomer;
begin
FService.CreateCustomer('João', '12345678901', 'joao@email.com');
LCustomer := FService.GetById(1);
try
Assert.IsNotNull(LCustomer);
Assert.AreEqual('João', LCustomer.Name);
finally
LCustomer.Free;
end;
end;
[Test]
procedure TCustomerServiceTest.GetById_WithInvalidId_ShouldRaiseNotFoundException;
begin
Assert.WillRaise(
procedure
begin
FService.GetById(999);
end,
EEntityNotFoundException
);
end;
initialization
TFPCUnit.RegisterTestFixture(TCustomerServiceTest);
end.
unit MeuApp.Tests.Helpers.Mocks;
interface
uses
System.SysUtils,
System.Generics.Collections,
MeuApp.Domain.Customer.Entity,
MeuApp.Domain.Customer.Repository.Intf;
type
TMemoryCustomerRepository = class(TInterfacedObject, ICustomerRepository)
private
FItems: TObjectList<TCustomer>;
FNextId: Integer;
public
constructor Create;
destructor Destroy; override;
function FindById(AId: Integer): TCustomer;
function FindAll: TObjectList<TCustomer>;
function FindByCpf(const ACpf: string): TCustomer;
function Exists(AId: Integer): Boolean;
procedure Insert(ACustomer: TCustomer);
procedure Update(ACustomer: TCustomer);
procedure Delete(AId: Integer);
end;
implementation
constructor TMemoryCustomerRepository.Create;
begin
inherited Create;
FItems := TObjectList<TCustomer>.Create(False);
FNextId := 1;
end;
destructor TMemoryCustomerRepository.Destroy;
begin
FItems.Free;
inherited;
end;
procedure TMemoryCustomerRepository.Insert(ACustomer: TCustomer);
begin
ACustomer.Id := FNextId;
Inc(FNextId);
FItems.Add(ACustomer);
end;
// ... demais métodos seguem o mesmo padrão
MethodName_Scenario_ExpectedBehavior
| Exemplo | Significado |
|---|---|
CreateCustomer_WithValidData_ShouldSucceed | Cenário feliz |
CreateCustomer_WithEmptyName_ShouldRaiseException | Validação |
GetById_WithInvalidId_ShouldRaiseNotFoundException | Not found |
Delete_WhenItemHasDependencies_ShouldRaiseException | Regra de negócio |
// Igualdade
Assert.AreEqual(Expected, Actual);
Assert.AreEqual('João', LCustomer.Name);
// Nulidade
Assert.IsNotNull(LCustomer);
Assert.IsNull(LResult);
// Booleanos
Assert.IsTrue(LCustomer.IsActive);
Assert.IsFalse(LOrder.IsCancelled);
// Exceptions
Assert.WillRaise(AnonProc, EValidationException);
Assert.WillNotRaiseAny(AnonProc);
// Contagem
Assert.AreEqual(3, LList.Count);
[TestFixture]
TCustomerRepositoryIntegrationTest = class
private
FConnection: TSQLConnection;
FRepository: ICustomerRepository;
public
[Setup]
procedure Setup;
[TearDown]
procedure TearDown;
end;
procedure TCustomerRepositoryIntegrationTest.Setup;
begin
FConnection := TSQLConnection.Create(nil);
FConnection.DriverName := 'SQLite';
FConnection.Params.Database := ':memory:';
FConnection.Connected := True;
// Criar schema
FConnection.ExecSQL(
'CREATE TABLE customers (' +
' id INTEGER PRIMARY KEY AUTOINCREMENT,' +
' name TEXT NOT NULL,' +
' cpf TEXT NOT NULL,' +
' email TEXT,' +
' status INTEGER DEFAULT 0' +
')'
);
FRepository := TSQLdb/ZeosCustomerRepository.Create(FConnection);
end;
procedure TCustomerRepositoryIntegrationTest.TearDown;
begin
FConnection.Free;
end;
MethodName_Scenario_ExpectedBehavior?:memory: usado para testes de integração de Repository?