Sub topic 3
Adding mocktail and Creating Your First Mock
Assume you know Dart classes, abstract classes, Future, and basic flutter_test tests. You should also be comfortable passing a dependency through a constructor.
mocktail is a Dart package for creating test doubles. A test double is an object that replaces a real dependency during a test. A mock is a test double that can provide configured results and record how the code uses it.
Add mocktail to the project
Add mocktail as a development dependency. Tests need the package, but the application itself does not need it when it runs in production.
dev_dependencies:
flutter_test:
sdk: flutter
mocktail: ^1.0.4Run this command from the project directory:
flutter pub getKeep test-only packages under dev_dependencies. This keeps the production dependency graph smaller and makes the package's purpose clear.
Create a mock class
Suppose a service needs a repository. The repository represents a data source, such as an API or database.
import 'package:flutter_test/flutter_test.dart';
import 'package:mocktail/mocktail.dart';
class User {
const User(this.id, this.name);
final String id;
final String name;
}
abstract class UserRepository {
Future<User> fetchUser(String id);
Future<void> saveUser(User user);
Stream<User> watchUser(String id);
}
class GreetingService {
GreetingService(this._repository);
final UserRepository _repository;
Future<String> greet(String id) async {
final user = await _repository.fetchUser(id);
return 'Hello, ${user.name}!';
}
}
class MockUserRepository extends Mock implements UserRepository {}
void main() {
test('accepts a mock repository', () {
final repository = MockUserRepository();
final service = GreetingService(repository);
expect(service, isNotNull);
});
}MockUserRepository inherits behavior from Mock and declares that it implements UserRepository. mocktail uses this information to intercept calls made through the interface. No code generation or generated file is required.
The mock can stand anywhere that the real repository is expected:
| Production dependency | Test replacement |
|---|---|
| `UserRepository` | `MockUserRepository` |
| Real API or database | Configurable test behavior |
| Network or disk access | No external access |
The test creates the mock and injects it into GreetingService. This is constructor injection: a class receives its dependency through its constructor instead of creating the dependency internally.
Why the constructor matters
This design keeps GreetingService focused on greeting users. It does not know whether users come from an HTTP client, a database, or a mock.
If GreetingService created its own repository, a test could not easily replace that repository. It would also risk making network or database calls. Injecting the dependency gives each environment control over the object used by the service.
> callout note > Define the mock next to the tests that use it, or place shared mocks in a test support file. Do not add mocktail imports to production files unless production code genuinely needs them.
At this stage, the mock exists but has no configured response. The next step is to stub its methods with when, thenReturn, thenAnswer, or thenThrow.
Exercise
Why does MockUserRepository use implements UserRepository in addition to extending Mock?
Show answer
It gives the mock the same interface as the production repository, so it can be injected wherever a UserRepository is required.