Mocks, Fakes, and Coverage
Isolate units and measure what you test.
Mocks, Fakes, and Coverage is a free Dart Academy lesson on CoddyKit — lesson 4 of 4. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the Dart Academy learning path, one of 4 lessons in the course, and your progress syncs across the web and the CoddyKit app.
Isolate the Unit
A unit test should check one piece in isolation. Real databases or networks make tests slow and flaky, so we replace them. 🔌
Test Doubles
A test double stands in for a real dependency. Mocks, fakes, and stubs are the common flavors you reach for.
What a Fake Is
A fake has a working but simplified implementation, like an in-memory list standing in for a real database.
class FakeRepo implements Repo {
final _items = <Item>[];
}What a Mock Is
A mock records how it was called so you can verify interactions, not just final values.
The mockito Package
The mockito package generates mocks for you. Annotate the type you want and run build_runner to create it.
@GenerateMocks([Repo])
import 'repo_test.mocks.dart';Stubbing With when
Use when to script a mock: when this method is called, return this value, so behavior is predictable.
when(repo.find(1))
.thenReturn(Item('a'));Verifying Calls
Use verify to assert a method was called, and how often, proving your code talked to its dependency correctly.
verify(repo.save(any)).called(1);Stubbing Async
For methods that return a Future, script them with thenAnswer so the mock can return an async result.
when(api.load())
.thenAnswer((_) async => 'ok');Mock vs Fake
Reach for a fake when you need realistic behavior, and a mock when you care about which calls happened.
What Coverage Means
Coverage measures which lines your tests actually run. High coverage hints at thoroughness, but never guarantees correctness.
Generating Coverage
Run tests with coverage collection, then turn the data into a report you can read or upload to a tool.
dart test --coverage=coverageQuick Check
Which double records calls so you can verify them?
Recap
You isolated units with fakes and mocks, stubbed with when, verified calls, and measured coverage. Fast, focused, trustworthy tests. 🎯
Frequently asked questions
Is the “Mocks, Fakes, and Coverage” lesson free?
Yes — the full text of “Mocks, Fakes, and Coverage” is free to read here on the web, and the Dart Academy course includes 4 lessons in total. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the Dart Academy course, upgrade to CoddyKit PRO.
What will I learn in “Mocks, Fakes, and Coverage”?
Isolate units and measure what you test. You practise Dart Academy with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.
Do I need any experience to start Dart Academy?
No prior experience is required. Dart Academy on CoddyKit is structured for beginners through advanced learners; this is — lesson 4 of 4, so you can start here or from the beginning and move at your own pace.
How long does the “Mocks, Fakes, and Coverage” lesson take?
Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.
Can I write and run code in this Dart Academy lesson?
Yes. Every Dart Academy lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.
All lessons in this course
- Your First test and expect
- group, setUp, and tearDown
- Testing async Code and Streams
- Mocks, Fakes, and Coverage