Unit Tests - RobotNet10.RobotManager
Thư mục này chứa các unit tests cho Robot Management module.
📁 Cấu trúc Tests
RobotNet10.RobotManager.Test/
├── Helpers/
│ └── TestHelpers.cs # Helper methods để tạo test data
├── Services/
│ ├── RobotModelServiceTests.cs # Tests cho RobotModelService
│ ├── RobotServiceTests.cs # Tests cho RobotService
│ └── RobotModelImageStorageServiceTests.cs # Tests cho Image Storage Service
├── README.md # File này
└── RobotNet10.RobotManager.Test.csproj
🧪 Test Coverage
✅ Đã được test
-
RobotModelService
- CreateAsync với valid data
- CreateAsync với duplicate model name (throws exception)
- GetAllAsync
- GetByIdAsync (existing và non-existent)
- UpdateAsync (valid data và non-existent ID)
- DeleteAsync (existing, non-existent, và model với robots)
- SearchAsync
- ExistsAsync
- GetUsageInfoAsync
-
RobotService
- CreateAsync với valid data
- CreateAsync với duplicate robot ID (throws exception)
- CreateAsync với non-existent model ID (throws exception)
- GetAllAsync (với và không có filters)
- GetByIdAsync
- GetByRobotIdAsync
- UpdateAsync
- DeleteAsync
- GetByModelIdAsync
- SearchAsync
- ExistsAsync
-
RobotModelImageStorageService
- SaveImageAsync
- GetImageAsync
- DeleteImageAsync
- ImageExistsAsync
- GetImageDimensionsAsync
🚀 Chạy Tests
Sử dụng Visual Studio
- Mở Test Explorer (Test → Test Explorer)
- Build solution
- Chọn tests cần chạy và click "Run"
Sử dụng dotnet CLI
cd srcs/RobotNet10/Tests/RobotNet10.RobotManager.Test
dotnet test
Chạy tests cụ thể
# Chạy tests trong một class
dotnet test --filter "FullyQualifiedName~RobotModelServiceTests"
# Chạy một test method cụ thể
dotnet test --filter "FullyQualifiedName~CreateAsync_ValidData_ReturnsCreatedRobotModel"
Chạy với output chi tiết
dotnet test --verbosity normal
Chạy với code coverage
dotnet test --collect:"XPlat Code Coverage"
📊 Test Statistics
- Total Tests: ~30+ test cases
- Test Framework: NUnit
- Coverage Areas:
- RobotModelService: CRUD operations, validation, business rules
- RobotService: CRUD operations, validation, filtering
- RobotModelImageStorageService: File operations
🛠️ Test Helpers
TestHelpers.cs cung cấp các helper methods để tạo test data:
CreateTestRobotModel()- Tạo RobotModel entity với các parameters tùy chọnCreateTestRobot()- Tạo Robot entity với các parameters tùy chọnSeedTestData()- Seed test data vào database context
📝 Best Practices
- AAA Pattern: Arrange, Act, Assert
- Test Isolation: Mỗi test độc lập, sử dụng InMemory database riêng
- Meaningful Names: Tên test mô tả rõ behavior được test
- Test Data: Sử dụng TestHelpers để tạo consistent test data
- Edge Cases: Test cả success và failure scenarios
🔍 Test Categories
Unit Tests
- Test từng service riêng biệt
- Sử dụng InMemory database để test database operations
- Mock external dependencies (IWebHostEnvironment, ILogger)
- Fast execution
Integration Tests (Future)
- Test interaction giữa các components
- Test với real database (optional)
- Test end-to-end scenarios
📈 Coverage Goals
- RobotModelService: Core methods ~90%
- RobotService: Core methods ~90%
- RobotModelImageStorageService: File operations ~80%
- Controllers: Pending (có thể thêm sau)
- SignalR Hub: Pending (cần integration test)
🐛 Known Issues
- Image dimension tests sử dụng invalid image data (cần valid PNG để test đầy đủ)
- Controller tests chưa được implement (có thể thêm sau với integration tests)
🤝 Contributing
Khi thêm tests mới:
- Follow naming convention:
MethodName_Scenario_ExpectedBehavior - Sử dụng TestHelpers cho test data
- Đảm bảo test isolation (mỗi test có database riêng)
- Test cả success và failure cases