Sub topic 10
Keeping Flutter Tests Isolated with setUp and reset
This chapter assumes that you know Dart classes, Future, Stream, flutter_test, and constructor-based dependency injection. It also assumes that you have created mocktail mocks and can stub methods with when.
Test isolation means that one test cannot change the result of another test. Mock objects have two kinds of state:
- Stubs, which describe what a method should return.
- Interactions, which record calls made to the mock.
If tests share either kind of state, a test may pass or fail because of the order in which tests run. Each test should start with predictable dependencies.
Create fresh mocks with setUp
setUp is a test callback that runs before every test in the surrounding group or main function. Create a new mock inside it. This gives every test a clean object with no old stubs or recorded calls.
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);
}
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() {
late MockUserRepository repository;
late GreetingService service;
setUp(() {
repository = MockUserRepository();
service = GreetingService(repository);
});
test('greets the returned user', () async {
when(() => repository.fetchUser('42'))
.thenAnswer((_) async => const User('42', 'Ada'));
expect(await service.greet('42'), 'Hello, Ada!');
verify(() => repository.fetchUser('42')).called(1);
});
test('can use a different result without affecting the first test', () async {
when(() => repository.fetchUser('42'))
.thenAnswer((_) async => const User('42', 'Grace'));
expect(await service.greet('42'), 'Hello, Grace!');
verify(() => repository.fetchUser('42')).called(1);
});
}The two tests use the same variable names, but they receive different mock instances. The stub for Ada cannot remain active when the second test begins.
Use reset when a mock must be reused
Usually, a fresh mock from setUp is clearer than resetting one manually. Sometimes a test contains phases that need different behavior. In that case, reset removes both the mock's stubs and its recorded interactions.
test('can replace behavior between phases', () async {
final repository = MockUserRepository();
when(() => repository.fetchUser('42'))
.thenAnswer((_) async => const User('42', 'Ada'));
expect(await repository.fetchUser('42'), const User('42', 'Ada'));
reset(repository);
when(() => repository.fetchUser('42'))
.thenAnswer((_) async => const User('42', 'Grace'));
expect(await repository.fetchUser('42'), const User('42', 'Grace'));
});After reset(repository), the original stub is gone. The call made before the reset is also no longer available to verify.
Use clearInteractions when you want to keep the current stubs but start a new verification phase.
| Operation | Removes stubs | Removes recorded calls |
|---|---|---|
| `reset(mock)` | Yes | Yes |
| `clearInteractions(mock)` | No | Yes |
Avoid shared mutable setup
Do not configure a mock once in setUpAll and reuse it across tests. setUpAll runs only once, so its mock can carry state from one test to another. It is useful for one-time setup, such as registering a fallback value, but not for ordinary per-test mocks.
Keep stubbing close to the test that needs it. This makes each test's assumptions visible and prevents an unused default stub from hiding a missing setup.
Exercise
A test calls clearInteractions(repository) and then calls repository.fetchUser('42'). Will the existing stub still determine the return value?
Show answer
Yes. clearInteractions removes recorded calls but keeps stubs. Use reset(repository) when both the stubs and interactions must be removed.