dart-generate-test-mocks

Generate mockito mock classes and unit tests for Dart dependencies using build_runner.

1|Updated Aug 14, 2026
One-click install
npx skills add https://github.com/sohampawar1866/zeromile-go --skill dart-generate-test-mocks-sohampawar1866
Or copy as Structured Prompt for Agent
Please help me install this Agent Skill.
Skill: dart-generate-test-mocks
Source: https://github.com/sohampawar1866/zeromile-go/tree/main/.agents/skills/dart-generate-test-mocks
Command: npx skills add https://github.com/sohampawar1866/zeromile-go --skill dart-generate-test-mocks-sohampawar1866

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 tedious manual mock creation, and incorrect stubbing of asynchronous methods causes confusing test failures. ## Core Features & Use Cases - Automatic Mock Generation: Uses @GenerateNiceMocks with package:mockito and build_runner to generate mock classes for injected dependencies. - Correct Async Stubbing: Enforces thenAnswer for Future and Stream returns, avoiding the common thenReturn ArgumentError pitfall. - Structured Test Workflow: Provides a 10-step checklist covering dependency injection, mock generation, stubbing, verification with verify() and argument matchers, and running dart test. - Use Case: When testing an ApiService class that calls http.Client, generate a MockClient, stub its GET responses, and verify the exact Uri it was called with. ## 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 mockito mocks in Dart?

Annotate your test file with @GenerateNiceMocks([MockSpec<YourType>()]), import the generated .mocks.dart file, then run dart run build_runner build. The mock classes are generated automatically and can be instantiated in your tests.

How to mock http.Client in Dart unit tests?

Inject http.Client through your class constructor, then generate a MockClient with @GenerateNiceMocks([MockSpec<http.Client>()]). Stub responses with when(mockClient.get(any)).thenAnswer((_) async => http.Response(...)) and verify calls with verify().

What is the difference between @GenerateMocks and @GenerateNiceMocks?

@GenerateNiceMocks is preferred because it avoids missing stub exceptions that @GenerateMocks throws when an unstubbed method is called. Nice mocks return benign default values instead of failing the test unexpectedly.

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

The error occurs when thenReturn is used for methods returning a Future or Stream. Replace it with thenAnswer((_) async => value) so the stub returns the correct asynchronous type.

Why does build_runner fail to generate .mocks.dart files?

Build failures usually come from a mismatched import path for the .mocks.dart file or syntax errors in the test file. Ensure the import name matches the test file name exactly and rerun dart run build_runner build.