Sub topic 9
Capturing Arguments to Inspect Them
This chapter assumes you know Dart classes, Future, setUp, expect, and basic mocktail stubbing. You will learn how to inspect the exact values that production code sends to a mocked dependency.
Why capture arguments?
A call count tells you that a method ran. It does not tell you whether the method received the correct object.
Suppose a service creates a User before saving it. The useful test is not only “did saveUser run?” It is also “did it save the expected name and ID?”
captureAny() is a mocktail argument matcher that records an argument during verification. The recorded values are available through the verification result's captured property.
Capturing one argument
The following complete test defines a repository, a mock, and a service. The service saves a user with a normalized name.
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<void> saveUser(User user);
}
class MockUserRepository extends Mock implements UserRepository {}
class ProfileService {
ProfileService(this._repository);
final UserRepository _repository;
Future<void> saveProfile(String id, String name) {
final user = User(id, name.trim());
return _repository.saveUser(user);
}
}
class FakeUser extends Fake implements User {}
void main() {
setUpAll(() {
registerFallbackValue(FakeUser());
});
test('saves the normalized user', () async {
final repository = MockUserRepository();
final service = ProfileService(repository);
when(() => repository.saveUser(any())).thenAnswer((_) async {});
await service.saveProfile('7', ' Grace Hopper ');
final captured = verify(
() => repository.saveUser(captureAny()),
).captured.single as User;
expect(captured.id, '7');
expect(captured.name, 'Grace Hopper');
});
}The test follows this order:
- Stub the repository method so the call can complete.
- Run the service operation.
- Verify the call with
captureAny(). - Read the single captured value.
- Assert on the fields that matter.
captured is a list because the method might have been called more than once. .single asserts that exactly one value was captured and returns that value.
Capturing multiple calls
When a method is called repeatedly, inspect all captured arguments. The list preserves call order.
test('saves each user in order', () async {
final repository = MockUserRepository();
when(() => repository.saveUser(any())).thenAnswer((_) async {});
await repository.saveUser(const User('1', 'Ada'));
await repository.saveUser(const User('2', 'Grace'));
final users = verify(
() => repository.saveUser(captureAny()),
).captured.cast<User>();
expect(users.map((user) => user.id), ['1', '2']);
});Use .first, .last, or a loop when several calls are expected. If the order itself matters, verifyInOrder can check the calls, while captured lets you inspect their contents.
| Need | Useful assertion |
|---|---|
| One exact object | `captured.single` |
| Several arguments | `captured.cast<User>()` |
| Call order | `verifyInOrder` |
| Only the number of calls | `verify(...).called(2)` |
Fallback values for captured custom types
Mocktail needs a fallback value when any() or captureAny() represents a custom non-nullable type such as User. Register a Fake once in setUpAll.
The fake is only a placeholder used while mocktail records the invocation. It does not replace the real User captured from the call.
Common mistake
Do not try to read captured values before verification. captureAny() records arguments as part of a verify call:
final values = verify(
() => repository.saveUser(captureAny()),
).captured;If the method was never called, verification fails before captured is returned. If it was called more times than expected, use the list rather than .single.
Exercise
What does verify(() => repository.saveUser(captureAny())).captured.single return when the repository received one User object?
Show answer
It returns that one captured User object, after the value is cast to User if needed.