Sub topic 8
Registering fallback values for custom types
This chapter assumes you know Dart classes, abstract classes, Future, async and await, and basic flutter_test tests. You should also know how to pass a dependency through a constructor and how to create a Mock class with mocktail.
Why fallback values are needed
Mocktail's argument matchers, such as any() and captureAny(), need a value of the expected type while the stub or verification expression is evaluated.
Mocktail already knows fallback values for common types such as String, int, and bool. It cannot create a value automatically for your own types, such as User or Payment. You must register a fallback value for each custom type that you use with a matcher.
A fallback value is only a placeholder. It is not the value returned by a stub, and it is not passed to the real implementation.
Creating a fake fallback
A mocktail Fake is a lightweight test double. For fallback registration, it usually does not need any behavior:
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 FakeUser extends Fake implements User {}
void main() {
setUpAll(() {
registerFallbackValue(FakeUser());
});
test('accepts any User', () async {
final repository = MockUserRepository();
when(() => repository.saveUser(any())).thenAnswer((_) async {});
await repository.saveUser(const User('7', 'Grace'));
verify(() => repository.saveUser(any())).called(1);
});
}setUpAll runs once before the tests in the group. Registering the fallback there avoids repeating the registration for every test. The registration applies to the current test isolate, so another test file may need its own registration.
The FakeUser instance is never used as a real user. Mocktail only needs it to know that any() represents a User.
Using `captureAny` with custom types
Use captureAny() when the fact that a call occurred is not enough. It lets you inspect the actual argument supplied to the mock.
test('saves the supplied user', () async {
final repository = MockUserRepository();
when(() => repository.saveUser(any())).thenAnswer((_) async {});
const user = User('7', 'Grace');
await repository.saveUser(user);
final captured = verify(
() => repository.saveUser(captureAny()),
).captured.single as User;
expect(captured.id, '7');
expect(captured.name, 'Grace');
});The stub still uses any() because the test does not require a particular User instance. The verification captures the real User passed by the code under test.
Named arguments and multiple custom types
For a named parameter, pass the parameter name to the matcher:
when(
() => repository.saveUser(any()),
);For a method with a named User parameter, the form would be:
when(
() => repository.saveNamedUser(user: any(named: 'user')),
);Register a fallback for every custom non-nullable type used with any, captureAny, or a related matcher. A missing registration usually produces an error explaining that mocktail could not find a fallback value.
| Matcher | Purpose | Needs a custom fallback? |
|---|---|---|
| `any()` | Matches any value of a type | Yes, for custom non-nullable types |
| `captureAny()` | Matches and records the actual value | Yes, for custom non-nullable types |
| `any(named: 'user')` | Matches a named argument | Yes, when the named type is custom |
Common mistake
Do not register User() directly unless the class has a suitable constructor and the instance is safe to create. A FakeUser is clearer because it communicates that the object is only a matcher placeholder.
Exercise
A test uses when(() => repository.saveUser(any())), but mocktail reports that it cannot find a fallback value for User. What should you add?
Show answer
Create a fake such as class FakeUser extends Fake implements User {} and call registerFallbackValue(FakeUser()), normally inside setUpAll.