Testing Pruebas

TestingPruebas

Firelands follows Test-Driven Development (TDD) for code quality.

Firelands sigue el Desarrollo Guiado por Pruebas (TDD) para la calidad del código.

Testing FrameworkFramework de Pruebas

  • Framework: GoogleTest (gtest/gmock)
  • Location: tests/unit/
  • Framework: GoogleTest (gtest/gmock)
  • Ubicación: tests/unit/

Building TestsConstruir Pruebas

cmake -B build -G Ninja -DFIRELANDS_BUILD_TESTS=ON
ninja -C build

Running TestsEjecutar Pruebas

# Run all tests
ctest --test-dir build

# Run tests matching pattern
ctest --test-dir build -R <pattern>

# Run specific test
ctest --test-dir build -R CharacterService
# Ejecutar todas las pruebas
ctest --test-dir build

# Ejecutar pruebas que coincidan con el patrón
ctest --test-dir build -R <pattern>

# Ejecutar prueba específica
ctest --test-dir build -R CharacterService

Test StructureEstructura de Pruebas

Tests are organized by layer:

  • tests/unit/domain/ - Domain entity tests
  • tests/unit/application/ - Service tests
  • tests/unit/infrastructure/ - Adapter tests
  • tests/unit/shared/ - Shared utilities tests

Las pruebas se organizan por capa:

  • tests/unit/domain/ - Pruebas de entidades del dominio
  • tests/unit/application/ - Pruebas de servicios
  • tests/unit/infrastructure/ - Pruebas de adaptadores
  • tests/unit/shared/ - Pruebas de utilidades compartidas

TDD WorkflowFlujo de TDD

  1. Red: Write failing test first
  2. Green: Write minimal code to pass
  3. Refactor: Clean up while keeping tests green
  1. Rojo: Escribir prueba que falle primero
  2. Verde: Escribir código mínimo para pasar
  3. Refactorizar: Limpiar manteniendo las pruebas verdes

Example TestEjemplo de Prueba

#include <gtest/gtest.h>
#include "CharacterService.h"

class CharacterServiceTest : public ::testing::Test {
protected:
    CharacterService service;
};

TEST_F(CharacterServiceTest, CreateCharacter_Success) {
    // Arrange
    PlayerCreateInfo createInfo;
    createInfo.race = RACE_HUMAN;
    createInfo.class_ = CLASS_WARRIOR;
    
    // Act & Assert
    EXPECT_TRUE(service.CanCreateCharacter(createInfo));
}

Arrange / Act & Assert

Preparar / Actuar & Verificar

MockingMocking

Use GMock to create mock implementations:

Usar GMock para crear implementaciones mock:

#include <gmock/gmock.h>
#include "CharacterRepository.h"

class MockCharacterRepository : public ICharacterRepository {
public:
    MOCK_METHOD(std::optional<Character>, FindById, (uint32 id), (override));
    MOCK_METHOD(bool, Save, (const Character& character), (override));
};