Sub topic 3
Registering fallback values
This chapter assumes you know Dart classes, can write package:test unit tests, understand Future, and have a Flutter project with mocktail in dev_dependencies.
any() matches any argument value. For built-in types like int and String, mocktail handles this automatically. For your own classes, mocktail needs a placeholder value of that type, called a fallback value, before the test runs.
Registering fallback values
Register the fallback once in setUpAll:
import 'package:mocktail/mocktail.dart';
import 'package:test/test.dart';
class User {
const User({required this.id});
final int id;
}
abstract class UserStore {
Future<void> save(User user);
}
class MockUserStore extends Mock implements UserStore {}
void main() {
setUpAll(() {
registerFallbackValue(const User(id: 0));
});
test('accepts any User', () async {
final store = MockUserStore();
when(() => store.save(any())).thenAnswer((_) async {});
await store.save(const User(id: 7));
verify(() => store.save(any())).called(1);
});
}Verifying calls
verify checks that a call happened. .called(1) asserts the exact count. Mocktail throws an error when the count differs, so a failing test points to the mismatch.
Capturing arguments
captureAny() records the value passed in, and .captured returns the recorded values as a list:
final captured = verify(() => store.save(captureAny())).captured;
expect((captured.single as User).id, 7);Mocking dependencies in widget tests
Pass the mock into the widget through its constructor, then pump the widget:
testWidgets('shows name from repository', (tester) async {
final repo = MockUserRepository();
when(() => repo.fetchName(1)).thenAnswer((_) async => 'Ada');
await tester.pumpWidget(MaterialApp(home: NameWidget(repo: repo)));
await tester.pumpAndSettle();
expect(find.text('Ada'), findsOneWidget);
});Resetting state between tests
Create mocks in setUp so each test starts with fresh stubs and call history:
late MockUserRepository repo;
setUp(() {
repo = MockUserRepository();
});flowchart TDRegistering fallback values
This chapter assumes you know Dart classes, can write package:test unit tests, understand Future, and have a Flutter project with mocktail in dev_dependencies.
any() matches any argument value. For built-in types like int and String, mocktail handles this automatically. For your own classes, mocktail needs a placeholder value of that type, called a fallback value, before the test runs.
Registering fallback values
Register the fallback once in setUpAll:
import 'package:mocktail/mocktail.dart';
import 'package:test/test.dart';
class User {
const User({required this.id});
final int id;
}
abstract class UserStore {
Future<void> save(User user);
}
class MockUserStore extends Mock implements UserStore {}
void main() {
setUpAll(() {
registerFallbackValue(const User(id: 0));
});
test('accepts any User', () async {
final store = MockUserStore();
when(() => store.save(any())).thenAnswer((_) async {});
await store.save(const User(id: 7));
verify(() => store.save(any())).called(1);
});
}Verifying calls
verify checks that a call happened. .called(1) asserts the exact count. Mocktail fails the test when the count differs, so the failure message shows the mismatch.
Capturing arguments
captureAny() records the value passed in, and .captured returns the recorded values as a list:
final captured = verify(() => store.save(captureAny())).captured;
expect((captured.single as User).id, 7);Mocking dependencies in widget tests
Pass the mock into the widget through its constructor, then pump the widget:
testWidgets('shows name from repository', (tester) async {
final repo = MockUserRepository();
when(() => repo.fetchName(1)).thenAnswer((_) async => 'Ada');
await tester.pumpWidget(MaterialApp(home: NameWidget(repo: repo)));
await tester.pumpAndSettle();
expect(find.text('Ada'), findsOneWidget);
});Resetting state between tests
Create mocks in setUp so each test starts with fresh stubs and call history:
late MockUserRepository repo;
setUp(() {
repo = MockUserRepository();
});Exercise
Why does verify(() => store.save(any())).called(1) fail if store.save was called twice?
Show answer
called(1) asserts exactly one matching call. Two calls make the count 2, so mocktail reports a mismatch.