179 lines
4.7 KiB
Markdown
179 lines
4.7 KiB
Markdown
# RobotNet10.RobotApp Tests
|
|
|
|
## ✅ Đã hoàn thành
|
|
|
|
### 1. Bugs đã được fix (trong phiên bản hiện tại của code)
|
|
- ✅ **Line 204**: Đã sửa từ `nodes[i].SequenceId` sang `edges[i].SequenceId`
|
|
- ✅ **Line 276**: Đã bỏ `.ToString()` thừa trên `LastNode.NodeId`
|
|
- ✅ **Code quality**: Tất cả bugs quan trọng đã được sửa
|
|
|
|
### 2. Test Infrastructure đã tạo
|
|
- ✅ Test project với xUnit và Moq
|
|
- ✅ Helper classes cho test data
|
|
- ✅ Comprehensive test cases cho validation logic
|
|
|
|
### 3. Test Coverage
|
|
Đã tạo tests cho:
|
|
- `ValidateNodes` - kiểm tra sequence validation
|
|
- `ValidateEdges` - kiểm tra edge validation
|
|
- `HandleNewOrder` - kiểm tra order processing
|
|
- `HandleUpdateOrder` - kiểm tra order updates
|
|
- Order control (Pause/Resume/Stop)
|
|
- NodeStates & EdgeStates tracking
|
|
|
|
## ⚠️ Vấn đề cần giải quyết
|
|
|
|
### Dependencies phức tạp
|
|
`RobotOrderController` có nhiều dependencies không thể mock dễ dàng:
|
|
|
|
1. **RobotConfiguration** - là BackgroundService, cần IServiceProvider
|
|
2. **RobotStateMachine** - cần Logger và RobotStateMachineExecute
|
|
3. **RobotStateMachineExecute** - cần IServiceProvider và Logger
|
|
|
|
### Giải pháp đề xuất
|
|
|
|
#### Approach 1: Refactor để dễ test hơn (Khuyến nghị)
|
|
```csharp
|
|
// Tạo interface cho RobotConfiguration
|
|
public interface IRobotConfiguration
|
|
{
|
|
Dictionary<SafetySpeed, double> SafetySpeedMap { get; }
|
|
string SerialNumber { get; }
|
|
// ... other properties
|
|
}
|
|
|
|
// Update RobotOrderController
|
|
public class RobotOrderController(
|
|
INavigation NavigationManager,
|
|
ILocalization Localization,
|
|
IAction ActionManager,
|
|
IError ErrorManager,
|
|
ISafety SafetyManager,
|
|
IRobotStateMachine StateManager, // Use interface
|
|
IRobotConfiguration RobotConfiguration, // Use interface
|
|
Logger<RobotOrderController> Logger) : IOrder
|
|
```
|
|
|
|
#### Approach 2: Integration Tests
|
|
Thay vì unit tests, viết integration tests với WebApplicationFactory:
|
|
|
|
```csharp
|
|
public class RobotOrderControllerIntegrationTests : IClassFixture<WebApplicationFactory<Program>>
|
|
{
|
|
private readonly WebApplicationFactory<Program> _factory;
|
|
|
|
public RobotOrderControllerIntegrationTests(WebApplicationFactory<Program> factory)
|
|
{
|
|
_factory = factory;
|
|
}
|
|
|
|
[Fact]
|
|
public async Task ValidateOrder_WithCorrectData_ShouldSucceed()
|
|
{
|
|
var controller = _factory.Services.GetRequiredService<IOrder>();
|
|
// ... test logic
|
|
}
|
|
}
|
|
```
|
|
|
|
#### Approach 3: Extract validation logic
|
|
Tách validation logic thành separate validator class:
|
|
|
|
```csharp
|
|
public class OrderValidator
|
|
{
|
|
public ValidationResult ValidateNodes(Node[] nodes, int currentSequence) { }
|
|
public ValidationResult ValidateEdges(Edge[] edges, Node[] nodes, int currentSequence) { }
|
|
}
|
|
|
|
// Trong RobotOrderController
|
|
private readonly OrderValidator _validator = new();
|
|
```
|
|
|
|
## 📊 Test Summary
|
|
|
|
| Test Category | Tests Written | Status |
|
|
|---------------|---------------|--------|
|
|
| ValidateNodes | 3 | ✅ Created |
|
|
| ValidateEdges | 5 | ✅ Created |
|
|
| HandleNewOrder | 3 | ✅ Created |
|
|
| HandleUpdateOrder | 4 | ✅ Created |
|
|
| Order Control | 3 | ✅ Created |
|
|
| State Tracking | 2 | ✅ Created |
|
|
| Safety Integration | 1 | ✅ Created |
|
|
| **TOTAL** | **21** | **Ready** |
|
|
|
|
## 🚀 Chạy tests (Sau khi refactor dependencies)
|
|
|
|
```bash
|
|
# Chạy tất cả tests
|
|
dotnet test
|
|
|
|
# Chạy tests với code coverage
|
|
dotnet test --collect:"XPlat Code Coverage"
|
|
|
|
# Chạy tests cụ thể
|
|
dotnet test --filter "FullyQualifiedName~ValidateNodes"
|
|
```
|
|
|
|
## 📝 Next Steps
|
|
|
|
1. **Refactor RobotConfiguration** → Tạo IRobotConfiguration interface
|
|
2. **Refactor RobotStateMachine** → Tạo IRobotStateMachine interface
|
|
3. **Update DI registration** → Register interfaces trong Program.cs
|
|
4. **Enable tests** → Uncomment và chạy tests sau khi refactor
|
|
5. **Add more tests** → Test edge cases và error scenarios
|
|
|
|
## 🎯 Test Best Practices
|
|
|
|
### DO ✅
|
|
- Test business logic riêng biệt
|
|
- Sử dụng meaningful test names
|
|
- Test một behavior per test
|
|
- Sử dụng AAA pattern (Arrange, Act, Assert)
|
|
- Mock external dependencies
|
|
|
|
### DON'T ❌
|
|
- Test implementation details
|
|
- Test framework code
|
|
- Có side effects giữa tests
|
|
- Hard-code test data
|
|
- Skip assertions
|
|
|
|
## 📚 Tài liệu tham khảo
|
|
|
|
- [xUnit Documentation](https://xunit.net/)
|
|
- [Moq Documentation](https://github.com/moq/moq4)
|
|
- [.NET Testing Best Practices](https://docs.microsoft.com/en-us/dotnet/core/testing/unit-testing-best-practices)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|