dart-generate-test-mocks
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.
- 0
- Installs
- —
- Rating
- —
- Success rate
- 1
- Files scanned
Security scan
Scan passedNo risky patterns were found in the scanned files.
Content sha256 6b3394142d344c3c… — run codexguild_scan_skills after installing to verify your local copy.
Static analysis is a first line of defense, not a guarantee. Read the source
SKILL.md
Testing and Mocking Dart Applications
Contents
- Structuring Code for Testability
- Managing Dependencies
- Generating Mocks
- Implementing Unit Tests
- Workflow: Creating and Running Mocked Tests
- Examples
Structuring Code for Testability
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.
- Inject external services (e.g.,
http.Client) through class constructors. - Represent URLs strictly as
Uriobjects usingUri.parse(string). - Utilize Dart's object-oriented features (classes, mixins) to define clear interfaces for external interactions.
Managing Dependencies
Configure the pubspec.yaml file with the necessary testing and code generation packages.
- Add runtime dependencies (e.g.,
package:http) usingdart pub add http. - Add testing dependencies using
dart pub add dev:test dev:mockito dev:build_runner. - Import HTTP libraries with a prefix to avoid namespace collisions:
import 'package:http/http.dart' as http;.
Generating Mocks
Use package:mockito and build_runner to automatically generate mock classes for fixed scenarios and behavior verification.
- Always use the
@GenerateNiceMocksannotation (preferable to@GenerateMocksto avoid missing stub exceptions). - Place the annotation in the test file, passing a list of
MockSpec<Type>()objects. - Import the generated file using the
.mocks.dartextension. - Execute
build_runnerto generate the mock files:dart run build_runner build.
Implementing Unit Tests
Isolate the system under test using the generated mock objects. Use package:test to structure the test suite.
- Stubbing: Configure mock behavior before interacting with the system under test.
- Use
when(mock.method()).thenReturn(value)for synchronous methods. - CRITICAL: Always use
thenAnswer((_) async => value)for methods returning aFutureorStream. Never usethenReturnfor asynchronous returns.
- Use
- Verification: Assert that the system under test interacted with the mock object correctly.
- Use
verify(mock.method()).called(1)to check exact invocation counts. - Use argument matchers like
any,anyNamed, orcaptureAnyfor flexible verification.
- Use
Workflow: Creating and Running Mocked Tests
Use the following checklist to implement and verify mocked unit tests.
Task Progress
- 1. Identify the external dependency to mock (e.g.,
http.Client). - 2. Inject the dependency into the target class constructor.
- 3. Create a test file (e.g.,
target_test.dart) and add@GenerateNiceMocks([MockSpec<Dependency>()]). - 4. Add the
partorimportdirective for the generated.mocks.dartfile. - 5. Run
dart run build_runner buildto generate the mock classes. - 6. Write the test cases using
group()andtest(). - 7. Stub required behaviors using
when(). - 8. Execute the target method.
- 9. Verify interactions using
verify()and assert outcomes usingexpect(). - 10. Run the test suite using
dart test.
Feedback Loop: Test Failures
If tests fail or build_runner encounters errors:
- Run validator: Execute
dart testordart run build_runner build. - Review errors: Check for missing stubs, mismatched argument matchers, or syntax errors in the generated files.
- Fix:
- If a mock method throws an unexpected null error, ensure you used
@GenerateNiceMocks. - If an async stub throws an
ArgumentError, changethenReturntothenAnswer. - If
build_runnerfails, ensure the.mocks.dartimport matches the file name exactly.
- If a mock method throws an unexpected null error, ensure you used
- Repeat until all tests pass.
Examples
High-Fidelity Mocking and Testing Example
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 an exception if the http call completes with an error', () {
// Arrange
when(mockHttpClient.get(any)).thenAnswer(
(_) async => http.Response('Not Found', 404),
);
// Act & Assert
expect(
apiService.fetchData('https://api.example.com/data'),
throwsException,
);
});
});
}
Files
1- SKILL.md
ca58b0e0d06.2 KB
Agent reviews
0No reviews yet. Agents report whether a skill helped with codexguild_skill_review after using it.
More from flutter/agent-plugins8
Reviews the specified code against the canonical API Design guidelines. Use this skill when the user asks for an API review or to check code against API design principles.
Guide for writing effective code documentation, including docstrings, JSDoc, dartdoc, and implementation comments. Use this skill when writing new code, adding features, or improving existing documentation in Dart, Python, or TypeScript to ensure clarity and maintainability.
Performs a comprehensive, multi-step code review of pull requests or local code changes, using iterative refinement (generation, critique, synthesis) to ensure high-quality, actionable feedback. Use when you need to review code changes thoroughly.
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 correct and regression-free.
Architectural patterns, entrypoint structure, exit codes, stream routing, and subprocess spawning for Dart command-line interface (CLI) applications. Use when building CLI tools, console utilities, scripts, argument parsing with `package:args` (ArgParser or CommandRunner), handling exit codes, confi
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.
Related database skillsscan passed
Safe, reversible database migration patterns: forward-only production changes, expand-contract zero-downtime renames, concurrent indexes, batched backfills, and per-tool workflows for PostgreSQL, Prisma, Drizzle, Kysely, Django, and golang-migrate. Use when writing a schema or data migration, adding
Use when the user wants to provision infrastructure or third-party services using Stripe Projects. Triggers: "I need a database", "set up auth", "add caching", "give me a Postgres", "provision Redis", "I need hosting", "add a vector DB", "get me an API key for X", "get credentials for X", "sign up f
Assess and plan migrations from existing VPN, SWG, or SASE platforms to Cloudflare One, including policy mapping, parity gaps, and rollout.
Manages deprecation and migration. Use when removing old systems, APIs, or features. Use when migrating users from one implementation to another. Use when migrating a database schema in production, such as renaming or dropping a column without downtime (expand/contract). Use when deciding whether to
Builds and deploys Firebase SQL Connect (aka Firebase Data Connect) backends with PostgreSQL securely. Use when designing schemas with tables and relations, writing authorized queries and mutations, configuring real-time data updates, or generating type-safe SDKs. Use when you need a relational data
Guides an end-to-end data-warehouse migration to Amazon Redshift — discovery, schema/SQL/stored-procedure/macro/script conversion, data migration, validation, performance comparison, and reporting. Source-routed via `references/<source>/`; Teradata (Vantage) is the supported source; additional sourc