Define and generate mock objects for external dependencies using `package:mockito` and `build_runner`. Use when unit testing classes that depend on complex external services like APIs or databases.
The dart-generate-test-mocks skill provides a structured approach to defining and generating mock objects in Dart applications, leveraging the `package:mockito` and `build_runner` tools. This skill is designed to simplify the process of unit testing classes that depend on complex external services, such as APIs or databases. By automating mock generation and enforcing best practices for testable code structures, it addresses the common challenges developers face when isolating dependencies, reducing the risk of brittle tests and ensuring consistent verification of interactions with external services.
The skill enables several key capabilities for Dart developers. It encourages designing classes for dependency injection, using strict type representations (e.g., `Uri` for URLs) and clear interfaces for external interactions. Dependencies are managed through proper `pubspec.yaml` configuration, separating runtime and testing dependencies. The skill automates mock generation using `@GenerateNiceMocks` annotations and `build_runner`, ensuring reliable asynchronous handling and verification. It also provides structured guidance for stubbing and verifying mock behaviors, including handling `Future` and `Stream` methods correctly, and offers a step-by-step workflow for creating, running, and troubleshooting mocked tests.
This skill is particularly useful for Dart developers engaged in unit testing of service-oriented or data-driven applications. It is ideal for teams aiming to enforce consistent testing patterns, reduce manual mock implementation, and improve test reliability. Typical use cases include testing HTTP client interactions, database operations, or any external service integration, where isolating the system under test is critical. By following this skill, developers can produce maintainable, robust tests that facilitate early bug detection and confidence in code correctness.
You place the `@GenerateNiceMocks` annotation in your test file with a list of `MockSpec<Type>()` objects, then run `dart run build_runner build` to generate the mock files, which are imported using the `.mocks.dart` extension.
Yes. For methods returning `Future` or `Stream`, always use `thenAnswer((_) async => value)` instead of `thenReturn` to correctly stub asynchronous behavior.
You need runtime dependencies such as `package:http` for external services, and dev dependencies including `test`, `mockito`, and `build_runner` for testing and mock generation.
It is compatible with classes designed for dependency injection and clear interface definitions. Complex external services must be isolated to allow replacement with mock objects during testing.
Run `dart test` or `dart run build_runner build`, review errors for missing stubs, argument mismatches, or syntax issues in generated files, and adjust the mock specifications or test code accordingly.
Quick Setup:
.claude/skills/Repository
dart-lang/skills