mockito

Uses the Mockito package for mocking in Flutter/Dart tests. Use when generating mocks, stubbing methods, verifying interactions, capturing arguments, or deciding between mocks, fakes, and real objects.

520 stars

Best use case

mockito is best used when you need a repeatable AI agent workflow instead of a one-off prompt. It is especially useful for teams working in multi. Uses the Mockito package for mocking in Flutter/Dart tests. Use when generating mocks, stubbing methods, verifying interactions, capturing arguments, or deciding between mocks, fakes, and real objects.

Uses the Mockito package for mocking in Flutter/Dart tests. Use when generating mocks, stubbing methods, verifying interactions, capturing arguments, or deciding between mocks, fakes, and real objects.

Users should expect a more consistent workflow output, faster repeated execution, and less time spent rewriting prompts from scratch.

Practical example

Example input

Use the "mockito" skill to help with this workflow task. Context: Uses the Mockito package for mocking in Flutter/Dart tests. Use when generating mocks, stubbing methods, verifying interactions, capturing arguments, or deciding between mocks, fakes, and real objects.

Example output

A structured workflow result with clearer steps, more consistent formatting, and an output that is easier to reuse in the next run.

When to use this skill

  • Use this skill when you want a reusable workflow rather than writing the same prompt again and again.

When not to use this skill

  • Do not use this when you only need a one-off answer and do not need a reusable workflow.
  • Do not use it if you cannot install or maintain the related files, repository context, or supporting tools.

Installation

Claude Code / Cursor / Codex

$curl -o ~/.claude/skills/mockito/SKILL.md --create-dirs "https://raw.githubusercontent.com/evanca/flutter-ai-rules/main/skills/mockito/SKILL.md"

Manual Installation

  1. Download SKILL.md from GitHub
  2. Place it in .claude/skills/mockito/SKILL.md inside your project
  3. Restart your AI agent — it will auto-discover the skill

How mockito Compares

Feature / AgentmockitoStandard Approach
Platform SupportNot specifiedLimited / Varies
Context Awareness High Baseline
Installation ComplexityUnknownN/A

Frequently Asked Questions

What does this skill do?

Uses the Mockito package for mocking in Flutter/Dart tests. Use when generating mocks, stubbing methods, verifying interactions, capturing arguments, or deciding between mocks, fakes, and real objects.

Where can I find the source code?

You can find the source code on GitHub using the link provided at the top of the page.

SKILL.md Source

# Mockito Skill

This skill defines how to correctly use the `mockito` package for mocking in Dart and Flutter tests.

---

## 1. Mock vs. Fake vs. Real Object

| Use | When |
|---|---|
| **Real object** | Prefer over mocks when practical. |
| **Fake** (`extends Fake`) | Lightweight custom implementation; override only the methods you need. Prefer over mocks when you don't need interaction verification. |
| **Mock** (`extends Mock`) | Only when you need to **verify interactions** (call counts, arguments) or stub dynamic responses. |

- **Data models** should not be mocked if they can be constructed with stubbed data.
- Only use mocks if your test has `verify` assertions; otherwise prefer real or fake objects.

---

## 2. Generating Mocks

```dart
@GenerateMocks([MyClass])
// or for nice mocks (return simple legal values for missing stubs):
@GenerateNiceMocks([MockSpec<MyClass>()])
void main() { ... }
```

```bash
dart run build_runner build
```

- Only annotate files under `test/` for mock generation by default.
- Use a `build.yaml` if you need to generate mocks outside of `test/`.
- Never add `@override` methods or implementations to a class extending `Mock`.
- Never stub responses in a mock's constructor or inside the mock class — always stub in your tests.

---

## 3. Stubbing

```dart
final mock = MockCat();

// Return a value
when(mock.sound()).thenReturn('Meow');

// Throw an error
when(mock.sound()).thenThrow(Exception('No sound'));

// Calculate response at call time
when(mock.sound()).thenAnswer((_) => computedValue);

// Return values in sequence
when(mock.sound()).thenReturnInOrder(['Meow', 'Purr']);
```

- Always stub methods/getters **before** using them if you need specific return values.
- Missing stub behavior: `@GenerateMocks` → throws; `@GenerateNiceMocks` → returns a simple legal value.
- Use `throwOnMissingStub(mock)` to throw on any unstubbed call.

---

## 4. Verification

```dart
verify(mock.sound());                  // called at least once
verifyNever(mock.eat(any));            // never called
verify(mock.sound()).called(2);        // called exactly twice
```

**Async:**

```dart
await untilCalled(mock.sound());       // wait for the interaction
```

---

## 5. Argument Matchers

```dart
// Flexible stubbing
when(mock.eat(any)).thenReturn(true);
when(mock.eat(argThat(isNotNull))).thenReturn(true);

// Named arguments
when(mock.fetch(any, headers: any)).thenReturn(response);
```

- Do **not** use `null` as an argument adjacent to an argument matcher.
- For named arguments, use `any` or `argThat` as values, not as argument names.

---

## 6. Capturing Arguments

```dart
final captured = verify(mock.eat(captureAny)).captured;
print(captured.last); // last captured argument
```

Use `captureThat` for conditional capturing.

---

## 7. Resetting Mocks

```dart
reset(mock);                  // clear all stubs AND interactions
clearInteractions(mock);      // clear only recorded interactions
```

---

## 8. Mocking Function Types

To mock a function type (e.g., a callback), define an abstract class with the required signature and generate mocks for it:

```dart
abstract class Callback {
  void call(String value);
}

@GenerateMocks([Callback])
```

---

## 9. Debugging

```dart
logInvocations([mock1, mock2]); // print all collected invocations
```

---

## References

- [Mockito GitHub Repository](https://github.com/dart-lang/mockito)

Related Skills

testing

520
from evanca/flutter-ai-rules

Writes and reviews Flutter/Dart tests. Use when writing unit tests, widget tests, or reviewing existing tests for correctness, structure, and naming conventions.

riverpod

520
from evanca/flutter-ai-rules

Uses Riverpod for state management in Flutter/Dart. Use when setting up providers, combining requests, managing state disposal, passing arguments, performing side effects, testing providers, or applying Riverpod best practices.

provider

520
from evanca/flutter-ai-rules

Uses the Provider package for dependency injection and state management in Flutter. Use when setting up providers, consuming state, optimizing rebuilds, using ProxyProvider, or migrating from deprecated providers.

patrol-e2e-testing

520
from evanca/flutter-ai-rules

Generates and maintains end-to-end tests for Flutter apps using Patrol. Use when adding E2E coverage for new features, regression tests for UI bugs, or testing native interactions (permissions, system dialogs, deep links)

mocktail

520
from evanca/flutter-ai-rules

Uses the Mocktail package for mocking in Flutter/Dart tests. Use when creating mocks, stubbing methods, verifying interactions, registering fallback values, or deciding between mocks, fakes, and real objects.

flutterfire-configure

520
from evanca/flutter-ai-rules

Sets up Firebase for Flutter apps using FlutterFire CLI. Use when initializing a Firebase project, running flutterfire configure, initializing Firebase in main.dart, or configuring multiple app flavors.

flutter-errors

520
from evanca/flutter-ai-rules

Diagnoses and fixes common Flutter errors. Use when encountering layout errors (RenderFlex overflow, unbounded constraints, RenderBox not laid out), scroll errors, or setState-during-build errors.

flutter-app-architecture

520
from evanca/flutter-ai-rules

Provides best practices for Flutter app architecture, including layered architecture, data flow, state management patterns, and extensibility guidelines.

firebase-storage

520
from evanca/flutter-ai-rules

Integrates Firebase Cloud Storage into Flutter apps. Use when setting up Storage, uploading or downloading files, managing metadata, handling errors, or applying security rules.

firebase-remote-config

520
from evanca/flutter-ai-rules

Integrates Firebase Remote Config into Flutter apps. Use when setting up Remote Config, managing parameter defaults, fetching and activating values, implementing real-time updates, or handling throttling and testing.

firebase-messaging

520
from evanca/flutter-ai-rules

Integrates Firebase Cloud Messaging (FCM) into Flutter apps. Use when setting up push notifications, handling foreground/background messages, managing permissions, working with FCM tokens, or configuring platform-specific notification behavior.

firebase-in-app-messaging

520
from evanca/flutter-ai-rules

Integrates Firebase In-App Messaging into Flutter apps. Use when setting up in-app messaging, triggering or suppressing messages, managing user privacy and opt-in data collection, or testing campaigns.