dart-generate-test-mocks

Generate mockito-based mock classes for Dart unit tests using build_runner.

Updated Jul 23, 2025
One-click install
npx skills add https://github.com/derrik-fleming/dotfiles --skill dart-generate-test-mocks-derrik-fleming
Or copy as Structured Prompt for Agent▼
Please help me install this Agent Skill.
Skill: dart-generate-test-mocks
Source: https://github.com/derrik-fleming/dotfiles/tree/main/private_dot_agents/skills/dart-generate-test-mocks
Command: npx skills add https://github.com/derrik-fleming/dotfiles --skill dart-generate-test-mocks-derrik-fleming

SYSTEM DOCUMENTATION & REQUIREMENTS

What problem does it solve? Writing unit tests for Dart classes that depend on external services like HTTP APIs or databases requires mock objects, and hand-writing them is error-prone and tedious. This Skill guides the definition and code generation of mocks with package:mockito and build_runner. ## Core Features & Use Cases - Mock Generation: Uses @GenerateNiceMocks annotations and build_runner to produce .mocks.dart files for injected dependencies. - Stubbing and Verification: Covers when()/thenAnswer() stubbing for async methods and verify() interaction checks with argument matchers. - Use Case: When testing an ApiService class that takes an http.Client via constructor injection, generate a MockClient, stub its get() method to return a canned Response, and verify the correct Uri was requested. ## Quick Start Generate mockito mocks for the http.Client dependency in my Dart ApiService class and write unit tests that stub and verify its HTTP calls.

Frequently Asked Questions about dart-generate-test-mocks

High-intent search queries and answers about installing and using this skill.

FAQPage Schema
How do I generate mocks in Dart with mockito?▼

Add dev dependencies mockito and build_runner, annotate your test file with @GenerateNiceMocks([MockSpec<YourType>()]), import the generated .mocks.dart file, then run dart run build_runner build to produce the mock classes.

What is the difference between @GenerateMocks and @GenerateNiceMocks?▼

@GenerateNiceMocks is preferred because it avoids missing-stub exceptions by returning sensible default values for unstubbed methods. @GenerateMocks throws errors when a method is called without an explicit stub, making tests more brittle.

Why does my mockito stub throw an ArgumentError for async methods?▼

Using thenReturn with a Future or Stream value causes an ArgumentError. Always use thenAnswer((_) async => value) when stubbing methods that return a Future or Stream.

How do I verify mock method calls in Dart tests?▼

Use verify(mock.method()).called(n) to assert exact invocation counts. Combine with argument matchers like any, anyNamed, or captureAny for flexible verification of the arguments passed to the mock.

Why does build_runner fail to generate the .mocks.dart file?▼

Check that the import or part directive for the .mocks.dart file matches the test file name exactly, and that mockito and build_runner are listed as dev dependencies in pubspec.yaml. Then rerun dart run build_runner build.