Testability Guidelines
What makes a code testable?
Practices to follow
1. Use Dependency Injection
class UserService {
final HttpClient httpClient;
UserService(this.httpClient);
Future<User> getUser(String id) async {
final response = await httpClient.get('/users/$id');
return User.fromJson(response.data);
}
}2. Use Separation of Concerns
3. Avoid global and static states
4. Don't call another function within the same class.
5. Use SOLID Principles
Single Responsibility Principle (SRP)
A class/method should have only one reason to change.
Last updated
Was this helpful?