145 lines
4.4 KiB
Markdown
145 lines
4.4 KiB
Markdown
# 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
|
|
|
|
1. **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
|
|
|
|
2. **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
|
|
|
|
3. **RobotModelImageStorageService**
|
|
- SaveImageAsync
|
|
- GetImageAsync
|
|
- DeleteImageAsync
|
|
- ImageExistsAsync
|
|
- GetImageDimensionsAsync
|
|
|
|
## 🚀 Chạy Tests
|
|
|
|
### Sử dụng Visual Studio
|
|
1. Mở Test Explorer (Test → Test Explorer)
|
|
2. Build solution
|
|
3. Chọn tests cần chạy và click "Run"
|
|
|
|
### Sử dụng dotnet CLI
|
|
```bash
|
|
cd srcs/RobotNet10/Tests/RobotNet10.RobotManager.Test
|
|
dotnet test
|
|
```
|
|
|
|
### Chạy tests cụ thể
|
|
```bash
|
|
# 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
|
|
```bash
|
|
dotnet test --verbosity normal
|
|
```
|
|
|
|
### Chạy với code coverage
|
|
```bash
|
|
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ọn
|
|
- `CreateTestRobot()` - Tạo Robot entity với các parameters tùy chọn
|
|
- `SeedTestData()` - Seed test data vào database context
|
|
|
|
## 📝 Best Practices
|
|
|
|
1. **AAA Pattern**: Arrange, Act, Assert
|
|
2. **Test Isolation**: Mỗi test độc lập, sử dụng InMemory database riêng
|
|
3. **Meaningful Names**: Tên test mô tả rõ behavior được test
|
|
4. **Test Data**: Sử dụng TestHelpers để tạo consistent test data
|
|
5. **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
|
|
|
|
- [x] RobotModelService: Core methods ~90%
|
|
- [x] RobotService: Core methods ~90%
|
|
- [x] 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:
|
|
|
|
1. Follow naming convention: `MethodName_Scenario_ExpectedBehavior`
|
|
2. Sử dụng TestHelpers cho test data
|
|
3. Đảm bảo test isolation (mỗi test có database riêng)
|
|
4. Test cả success và failure cases
|