mockito

$npx mdskill add evanca/flutter-ai-rules/mockito

Generate and manage Mockito mocks for Dart and Flutter tests.

  • Creates realistic test doubles for verifying object interactions.
  • Integrates with build_runner to generate mock specifications.
  • Selects mock types based on verification needs versus performance.
  • Outputs runnable test code with stubbed methods and verified calls.

SKILL.md

.github/skills/mockitoView on GitHub ↗
---
name: mockito
description: 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.
---

# 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)

More from evanca/flutter-ai-rules

SkillDescription
architecture-feature-firstStructures Flutter apps using layered architecture (UI / Logic / Data) with feature-first file organization. Use when creating new features, designing the project structure, adding repositories/services/view models (or cubits/providers/notifiers), or wiring dependency injection. State management agnostic.
blocImplements Flutter state management using the bloc library (Bloc and Cubit). Use when creating new features, screens, or state management logic with bloc/cubit, modeling state, wiring Flutter widgets to blocs, or writing bloc/cubit unit tests.
dart-3-updatesApplies Dart 3 language features in Flutter/Dart code. Use when writing if-else or switch statements, creating new classes, or deciding between a data class and a record.
effective-dartApplies Effective Dart guidelines in Flutter/Dart code. Use when writing or reviewing Dart code for naming conventions, types, style, imports, file structure, usage patterns, documentation, testing, widgets, state management, or performance.
firebase-aiIntegrates Firebase AI Logic into Flutter apps. Use when setting up the firebase_ai plugin, calling Gemini models, handling AI service errors, or applying security and privacy considerations for AI features.
firebase-analyticsIntegrates Firebase Analytics into Flutter apps. Use when setting up analytics, logging events, setting user properties, or configuring event parameters.
firebase-app-checkIntegrates Firebase App Check into Flutter apps. Use when setting up App Check, selecting providers per platform, using debug providers during development, enabling enforcement, or applying App Check security best practices.
firebase-authIntegrates Firebase Authentication into Flutter apps. Use when setting up auth, managing auth state, implementing email/password or social sign-in, handling auth errors, managing users, or applying security best practices.
firebase-cloud-firestoreIntegrates Cloud Firestore into Flutter apps. Use when setting up Firestore, designing document/collection structure, reading and writing data, working with real-time listeners, designing for scale, or applying security rules.
firebase-cloud-functionsCalls Firebase Cloud Functions from Flutter apps. Use when setting up callable functions, passing data to functions, handling errors from function calls, optimizing performance, or testing with the Firebase Emulator Suite.