Sub topic 7
Verifying That Calls Happened
This chapter assumes you know Dart classes, Future, flutter_test, constructor-based dependency injection, and how to create a mocktail mock. The previous chapter introduced stubbing. Here, you will check whether the code under test called its dependency correctly.
Why verify interactions?
A unit test can check a returned value, but the value alone may not prove that the code followed the correct path. For example, a cached value might produce the expected result even when the repository was never called.
Interaction verification checks calls made to a mock:
- Was the dependency called?
- Was it called with the expected argument?
- Was it called exactly once?
- Was an unwanted method never called?
- Did several calls happen in the required order?
Verification tests behavior at the boundary between your class and its dependency. Keep these tests focused. Verify important interactions, not every internal detail.
Using `verify`
verify takes the same closure syntax used by when. Add called(1) when the number of calls matters.
| Mocktail method | Purpose |
|---|---|
| `verify` | Confirms that a matching call happened |
| `called(1)` | Requires an exact number of matching calls |
| `verifyNever` | Confirms that a matching call did not happen |
| `verifyInOrder` | Confirms that matching calls happened in sequence |
The following test verifies one repository call, an absent call, and the order of two 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);
Future<void> saveUser(User user);
}
class ProfileService {
ProfileService(this._repository);
final UserRepository _repository;
Future<User> load(String id) => _repository.fetchUser(id);
Future<void> refreshAndSave(String id) async {
final user = await _repository.fetchUser(id);
await _repository.saveUser(user);
}
}
class MockUserRepository extends Mock implements UserRepository {}
void main() {
late MockUserRepository repository;
late ProfileService service;
setUp(() {
repository = MockUserRepository();
service = ProfileService(repository);
});
test('verifies the repository call and its argument', () async {
when(() => repository.fetchUser('42'))
.thenAnswer((_) async => const User('42', 'Ada'));
await service.load('42');
verify(() => repository.fetchUser('42')).called(1);
});
test('verifies that saving was not requested', () async {
when(() => repository.fetchUser('42'))
.thenAnswer((_) async => const User('42', 'Ada'));
await service.load('42');
verifyNever(
() => repository.saveUser(const User('42', 'Ada')),
);
});
test('verifies the order of fetching and saving', () async {
const user = User('42', 'Ada');
when(() => repository.fetchUser('42')).thenAnswer((_) async => user);
when(() => repository.saveUser(user)).thenAnswer((_) async {});
await service.refreshAndSave('42');
verifyInOrder([
() => repository.fetchUser('42'),
() => repository.saveUser(user),
]);
});
}verifyInOrder does not require the calls to be consecutive. It requires the listed calls to appear in that order among the recorded interactions. Use it when order is part of the behavior, such as fetching data before saving it.
Common verification mistakes
A verification can fail for several reasons:
- The method was never called because the code took another branch.
- The argument differs from the value in the verification closure.
- The call happened more or fewer times than
calledrequires. - The verification runs before an asynchronous operation finishes.
- A previous verification already consumed the matching interaction.
For asynchronous code, await the operation before verifying it. If a method starts work without returning its Future, the test may need a better API design or another way to wait for completion.
Do not verify calls that are irrelevant to the behavior under test. Such checks make tests fragile during harmless refactoring.
Exercise
A service should call repository.fetchUser('7') once. Which verification checks both the argument and the call count?
Show answer
verify(() => repository.fetchUser('7')).called(1);