dart-add-unit-test
Write and organize unit tests for functions, methods, and classes using `package:test`. Use when creating new logic or fixing bugs to ensure code remains…
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.
$ npx -y skills add flutter/agent-plugins --skill dart-generate-test-mocks --agent claude-codeHow it fires
How this skill gets triggered: by you, by Claude, or both.
/dart-generate-test-mocksContext preview
The summary Claude sees to decide when to auto-load this skill.
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.
name: dart-generate-test-mocks description: 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. metadata: model: models/gemini-3.1-pro-preview last_modified: Fri, 24 Apr 2026 15:13:58 GMT
Design Dart classes to support dependency injection. Isolate complex external dependencies (like API clients or databases) so they can be replaced with mock objects during testing.
Configure the `pubspec.yaml` file with the necessary testing and code generation packages.
Use `package:mockito` and `build_runner` to automatically generate mock classes for fixed scenarios and behavior verification.
Isolate the system under test using the generated mock objects. Use `package:test` to structure the test suite.
Use the following checklist to implement and verify mocked unit tests.
If tests fail or `build_runner` encounters errors: 1. **Run validator:** Execute `dart test` or `dart run build_runner build`. 2. **Review errors:** Check for missing stubs, mismatched argument matchers, or syntax errors in the generated files. 3. **Fix:**
4. Repeat until all tests pass.
**1. System Under Test (`lib/api_service.dart`)**
import 'dart:convert';
import 'package:http/http.dart' as http;
class ApiService {
final http.Client client;
ApiService(this.client);
Future<String> fetchData(String urlString) async {
final uri = Uri.parse(urlString);
final response = await client.get(uri);
if (response.statusCode == 200) {
return jsonDecode(response.body)['data'];
} else {
throw Exception('Failed to load data');
}
}
}**2. Test Implementation (`test/api_service_test.dart`)**
import 'package:test/test.dart';
import 'package:mockito/annotations.dart';
import 'package:mockito/mockito.dart';
import 'package:http/http.dart' as http;
import 'package:my_app/api_service.dart';
// Generate the mock class for http.Client
@GenerateNiceMocks([MockSpec<http.Client>()])
import 'api_service_test.mocks.dart';
void main() {
group('ApiService', () {
late ApiService apiService;
late MockClient mockHttpClient;
setUp(() {
mockHttpClient = MockClient();
apiService = ApiService(mockHttpClient);
});
test('returns data if the http call completes successfully', () async {
// Arrange: Stub the async HTTP GET request using thenAnswer
when(mockHttpClient.get(any)).thenAnswer(
(_) async => http.Response('{"data": "Success"}', 200),
);
// Act
final result = await apiService.fetchData('https://api.example.com/data');
// Assert
expect(result, 'Success');
// Verify the mock was called with the correct Uri
verify(mockHttpClient.get(Uri.parse('https://api.example.com/data'))).called(1);
});
test('throws anAgent plugins for Flutter, maintained by the Flutter team. A collection of plugins designed to extend AI agent capabilities for Flutter development.
Write and organize unit tests for functions, methods, and classes using `package:test`. Use when creating new logic or fixing bugs to ensure code remains…
Entrypoint structure, exit codes, cross-platform scripts. Use when building command line utilities, scripts, or applications.
Collect coverage using the coverage packge and create an LCOV report
Uses get_runtime_errors and lsp to fetch an active stack trace, locate the failing line, apply a fix, and verify resolution via hot_reload.
Replace the usage of `expect` and similar functions from `package:matcher` to `package:checks` equivalents.
Workflow for fixing package version conflicts. Use this when `pub get` fails due to incompatible package versions.