# Dart generate test mocks

> Dart generate test mocks is an open-source agent skill for developer tools. Teaches agents to write Dart unit tests with generated Mockito mocks via build_runner. It is available on Claude Code, Codex, Cursor, Gemini CLI, GitHub Copilot, Windsurf and Goose.

- **Kind:** Skill
- **Pricing:** Open source
- **Platforms:** Claude Code, Codex, Cursor, Gemini CLI, GitHub Copilot, Windsurf, Goose
- **Website:** https://github.com/flutter/agent-plugins/tree/main/skills/dart-generate-test-mocks
- **Repository:** https://github.com/flutter/agent-plugins
- **GitHub stars:** 3,223
- **License:** BSD-3-Clause
- **Skill name:** dart-generate-test-mocks
- **Collection:** https://github.com/flutter/agent-plugins
- **Categories:** [developer tools](https://ai-powered.com/categories/developer-tools)
- **Listing updated:** 2026-11-30

## About

This skill guides an agent through testing Dart code that talks to outside services such as HTTP APIs or databases. It explains how to structure classes so their dependencies can be injected and swapped for test doubles, and how to set up the pubspec with test, mockito and build_runner as dev dependencies.

The workflow covers annotating a test file with GenerateNiceMocks and MockSpec, running build_runner to produce the .mocks.dart file, then stubbing with when and thenAnswer for asynchronous calls and checking interactions with verify. It also includes a troubleshooting loop for common failures such as unexpected null results or build_runner import mismatches, and a worked example that mocks an http.Client.

The skill assumes a Dart project and works with the test package, package:mockito and build_runner. It is part of the Flutter agent plugins repository and is released under the BSD-3-Clause license.

## When your agent uses it

> 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.

## What it can access

From a static check of the files on GitHub; nothing was run.

- [Instructions only, no scripts](https://github.com/flutter/agent-plugins/tree/HEAD/skills/dart-generate-test-mocks)
- [License: BSD-3-Clause](https://github.com/flutter/agent-plugins/tree/HEAD/skills/dart-generate-test-mocks)

## Features

- **Dependency injection setup:** Shows how to pass external clients like http.Client through constructors so they can be replaced during tests.
- **Mock generation with build_runner:** Covers annotating test files with GenerateNiceMocks and generating the mock classes with a single build command.
- **Async stubbing rules:** Explains when to use thenAnswer instead of thenReturn for Future and Stream results.
- **Interaction verification:** Describes using verify, call counts and argument matchers such as any and captureAny.
- **Test failure troubleshooting:** Lists common mock and build_runner errors and how to resolve them step by step.
- **Worked HTTP client example:** Provides a complete service class and test file that mock an http.Client with success and error responses.

## Good for

- Write unit tests for a class that calls a REST API in Dart
- Mock a database access layer so tests run without a real database
- Generate mock classes for a Dart service using build_runner
- Stub an asynchronous method and verify how many times it was called
- Fix a failing Dart test caused by a missing mock stub

## Install

### Claude Code

This skill ships in the `dart-flutter` plugin. In Claude Code, add the marketplace and install the plugin (it may include other skills too):

```sh
/plugin marketplace add flutter/agent-plugins
/plugin install dart-flutter@dart-flutter
```

Or copy only this skill to `~/.claude/skills/dart-generate-test-mocks`:

```sh
tmp=$(mktemp -d) &&
git clone --depth 1 https://github.com/flutter/agent-plugins.git "$tmp" &&
mkdir -p ~/.claude/skills &&
rm -rf ~/.claude/skills/dart-generate-test-mocks &&
cp -R "$tmp/skills/dart-generate-test-mocks" ~/.claude/skills/dart-generate-test-mocks &&
rm -rf "$tmp"
```

For one project only, put the folder in `.claude/skills/dart-generate-test-mocks` inside it instead.

### Codex

Copy the skill folder to `~/.agents/skills/dart-generate-test-mocks` to use it in every repository:

```sh
tmp=$(mktemp -d) &&
git clone --depth 1 https://github.com/flutter/agent-plugins.git "$tmp" &&
mkdir -p ~/.agents/skills &&
rm -rf ~/.agents/skills/dart-generate-test-mocks &&
cp -R "$tmp/skills/dart-generate-test-mocks" ~/.agents/skills/dart-generate-test-mocks &&
rm -rf "$tmp"
```

For one repository only, put the folder in `.agents/skills/dart-generate-test-mocks` inside it instead.

### Cursor

Copy the skill folder to `~/.cursor/skills/dart-generate-test-mocks` to use it in every project:

```sh
tmp=$(mktemp -d) &&
git clone --depth 1 https://github.com/flutter/agent-plugins.git "$tmp" &&
mkdir -p ~/.cursor/skills &&
rm -rf ~/.cursor/skills/dart-generate-test-mocks &&
cp -R "$tmp/skills/dart-generate-test-mocks" ~/.cursor/skills/dart-generate-test-mocks &&
rm -rf "$tmp"
```

For one project only, put the folder in `.cursor/skills/dart-generate-test-mocks` inside it instead.

### Gemini CLI

Run in a terminal (add `--scope workspace` to install it for the current workspace only):

```sh
gemini skills install https://github.com/flutter/agent-plugins.git --path skills/dart-generate-test-mocks
```

Or copy the folder to `~/.gemini/skills/dart-generate-test-mocks` yourself.

### VS Code

For GitHub Copilot in VS Code, copy the skill folder to `~/.copilot/skills/dart-generate-test-mocks`:

```sh
tmp=$(mktemp -d) &&
git clone --depth 1 https://github.com/flutter/agent-plugins.git "$tmp" &&
mkdir -p ~/.copilot/skills &&
rm -rf ~/.copilot/skills/dart-generate-test-mocks &&
cp -R "$tmp/skills/dart-generate-test-mocks" ~/.copilot/skills/dart-generate-test-mocks &&
rm -rf "$tmp"
```

For one project only, put the folder in `.github/skills/dart-generate-test-mocks` inside it instead.

### Other agents

Get the folder:

```sh
git clone --depth 1 https://github.com/flutter/agent-plugins.git
```

Then copy `agent-plugins/skills/dart-generate-test-mocks` into your agent's skills folder as `dart-generate-test-mocks`. Agents that support SKILL.md read one folder per skill.

## FAQ

### What packages does Dart generate test mocks require in a Dart project?

It uses package:test, package:mockito and build_runner as dev dependencies, plus any runtime packages such as package:http that the code under test depends on.

### Why does the skill recommend GenerateNiceMocks over GenerateMocks?

According to the skill, GenerateNiceMocks avoids missing stub exceptions, so unstubbed calls return default values instead of failing the test.

### Which mocking command does the skill use?

It runs dart run build_runner build to generate the mock files, and dart test to run the suite.

### When should thenAnswer be used instead of thenReturn?

The skill says to use thenAnswer with an async function for methods that return a Future or Stream, and not thenReturn.

### How do I install Dart generate test mocks in Claude Code?

Copy the skill folder from GitHub to ~/.claude/skills/dart-generate-test-mocks to use it in all your projects, or to .claude/skills/dart-generate-test-mocks in one project. Or install the dart-flutter plugin, which includes it: /plugin marketplace add flutter/agent-plugins, then /plugin install dart-flutter@dart-flutter.

### Does Dart generate test mocks run scripts?

No. It is instructions only, with no scripts. This comes from a static check of the files on GitHub; nothing was run. Read the files before you install it.

### Which AI agents can use Dart generate test mocks?

Claude Code, Codex, Cursor, Gemini CLI, GitHub Copilot, Windsurf and Goose. Each reads skills in the SKILL.md format from its own folder; the install steps on this page list them.

### What are the best alternatives to Dart generate test mocks?

The closest alternatives to Dart generate test mocks are Dart add unit test, Flutter testing skill, Dart flutter patterns, Flutter add widget test and Flutter add integration test.

## Where this comes from

- Description: from the skill's own SKILL.md.

## Alternatives

- [Dart add unit test](https://ai-powered.com/skills/dart-add-unit-test): Dart add unit test is an open-source agent skill for developer tools.
- [Flutter testing skill](https://ai-powered.com/skills/flutter-testing-skill): Flutter testing skill is an open-source agent skill for developer tools.
- [Dart flutter patterns](https://ai-powered.com/skills/dart-flutter-patterns): Dart flutter patterns is an open-source agent skill for developer tools.
- [Flutter add widget test](https://ai-powered.com/skills/flutter-add-widget-test): Flutter add widget test is an open-source agent skill for developer tools.
- [Flutter add integration test](https://ai-powered.com/skills/flutter-add-integration-test): Flutter add integration test is an open-source agent skill for developer tools.
- [Unittest skill](https://ai-powered.com/skills/unittest-skill): Unittest skill is an open-source agent skill for developer tools.
- [Dart migrate to checks package](https://ai-powered.com/skills/dart-migrate-to-checks-package): Dart migrate to checks package is an open-source agent skill for developer tools.
- [Dart collect coverage](https://ai-powered.com/skills/dart-collect-coverage): Dart collect coverage is an open-source agent skill for developer tools.

---
Source: https://ai-powered.com/skills/dart-generate-test-mocks
