Install
$ agentstack add skill-dhruvanbhalara-skills-dart-generate-test-mocks ✓ scanned · ✓ verified, works with Claude Code, Cursor, and more.
Security review
✓ PassedNo issues found. Passed automated security review. · v0.1.0 How review works →
- ✓ Prompt-injection patterns
- ✓ Secret / credential exfiltration
- ✓ Dangerous shell & filesystem operations
- ✓ Untrusted network calls
- ✓ Known-malicious package signatures
What it can access
- ✓ Network access No
- ✓ Filesystem access No
- ✓ Shell / process execution No
- ✓ Environment & secrets No
- ✓ Dynamic code execution No
From automated source analysis of v0.1.0. “Used” means the capability is present in the source — more access means more to trust, not that it’s unsafe.
Verified badge
Passed review? Show it. Paste this badge into your README, it links to the public security report.
Reliability & compatibility
Declared compatibility
Compatibility is declared by the source manifest. End-to-end runtime verification is coming, see below.
We're building live execution health for every listing: tool-call success rate, median latency, uptime, and last-checked timestamps, measured, not self-reported. It isn't live yet, so we don't show numbers we can't stand behind.
How agent discovery & health will work →About
Contents
- [Structuring Code for Testability](#structuring-code-for-testability)
- [Managing Dev Dependencies](#managing-dev-dependencies)
- [Generating Mock Files](#generating-mock-files)
- [Stubbing and Verification Best Practices](#stubbing-and-verification-best-practices)
- [Workflow: Generating Mocks and Validating Tests](#workflow-generating-mocks-and-validating-tests)
- [Examples](#examples)
Structuring Code for Testability
To write effective unit tests, structure your codebase using dependency injection. Isolate operations that interact with physical layers (like disk storage, external servers, and platform channels) so they can be replaced by mock objects at test time:
- Constructor Injection: Pass all service dependencies (e.g. HTTP clients, database helper clients) into class constructors rather than instantiating them directly within the class.
- Interface Segregation: Define clear, abstract base classes representing your service contracts. Mocks should ideally target these abstract contracts rather than concrete service implementations.
Managing Dev Dependencies
Configure your pubspec.yaml to specify the packages required for code generation and mock testing:
- Add target testing frameworks and generators under
dev_dependencies:
``bash dart pub add dev:test dev:mockito dev:build_runner ``
- Import Mockito annotations inside your test files:
``dart import 'package:mockito/annotations.dart'; import 'package:mockito/mockito.dart'; ``
Generating Mock Files
Leverage package:mockito alongside build_runner to generate mock structures automatically:
- GenerateNiceMocks: Always use the
@GenerateNiceMocksannotation instead of the legacy@GenerateMocks. Nice mocks automatically return null or matching default values instead of throwing "MissingStubException" when a method is invoked without a pre-configured stub. - MockSpec Configuration: Annotate your test file's entry point with
@GenerateNiceMocks([MockSpec()]). - Mirror Extension Import: Import the matching generated file using the
.mocks.dartsuffix (e.g.import 'service_test.mocks.dart'). - Run Generator: Trigger code generation via the CLI:
``bash dart run build_runner build --delete-conflicting-outputs ``
Stubbing and Verification Best Practices
Write robust mock interactions by adhering to these guidelines:
- Future and Stream Stubbing: When stubbing methods that return a
Futureor aStream, always use.thenAnswer((_) async => value). Never use.thenReturn()for asynchronous return values, as this causes runtime cast errors. - Invocation Tracking: Use the
verify()API to verify that specific methods were invoked. Call.called(number)to assert precise invocation counts. - Unused Assertions: Use
verifyNever()orverifyNoMoreInteractions(mock)to guarantee that no unexpected actions occurred during the test lifecycle.
Workflow: Generating Mocks and Validating Tests
Follow this checklist to establish mock configurations:
- [ ] Constructor check: Ensure the target service class accepts dependencies via its constructor.
- [ ] Write test file: Create
test/feature_test.dartand import Mockito along with testing libraries. - [ ] Annotate entrypoint: Add
@GenerateNiceMocks([MockSpec()])abovemain(). - [ ] Declare mock import: Add the import statement targeting
feature_test.mocks.dart. - [ ] Generate code: Run
dart run build_runner buildin your terminal to create the mocked file. - [ ] Instantiate mocks: In the
setUpcallback, instantiate the generated mock class (e.g.mockService = MockTargetService()). - [ ] Apply stubbing: Configure behavior in your test cases using
when(). - [ ] Execute and assert: Call the system under test, verifying outputs via
expect(). - [ ] Verify calls: Use
verify()to assert mock interactions.
Examples
Complete Mocked Test Suit (Mockito)
This example shows how to mock a remote API client to test a database synchronization service.
import 'package:test/test.dart';
import 'package:mockito/annotations.dart';
import 'package:mockito/mockito.dart';
import 'package:http/http.dart' as http;
// Define an abstract contract for a user repository
abstract class UserRepository {
Future getUserName(int id);
}
// System Under Test consuming the repository
class UserService {
final UserRepository repository;
UserService(this.repository);
Future fetchDisplayName(int id) async {
try {
final name = await repository.getUserName(id);
return 'User: $name';
} catch (_) {
return 'Unknown User';
}
}
}
// 1. Annotate to generate MockUserRepository
@GenerateNiceMocks([MockSpec()])
import 'user_service_test.mocks.dart';
void main() {
group('UserService', () {
late MockUserRepository mockRepo;
late UserService userService;
setUp(() {
mockRepo = MockUserRepository();
userService = UserService(mockRepo);
});
test('returns formatted display name when repository succeeds', () async {
// 2. Arrange: Stub the async getUserName method
when(mockRepo.getUserName(42)).thenAnswer(
(_) async => 'Alice',
);
// 3. Act: Run target method
final displayName = await userService.fetchDisplayName(42);
// 4. Assert: Validate outcomes
expect(displayName, equals('User: Alice'));
// 5. Verify: Assert the repository method was invoked with correct arguments
verify(mockRepo.getUserName(42)).called(1);
});
test('returns fallback string when repository throws exception', () async {
// Arrange: Stub to throw error
when(mockRepo.getUserName(any)).thenThrow(
Exception('Connection Timeout'),
);
// Act
final displayName = await userService.fetchDisplayName(99);
// Assert
expect(displayName, equals('Unknown User'));
});
});
}
Source & license
This open-source skill is cataloged on AgentStack and links to its original source — we do not rehost the code.
- Author: dhruvanbhalara
- Source: dhruvanbhalara/skills
- License: MIT
- Homepage: https://dhruvanbhalara.github.io/skills/
Install and usage instructions live in the source repository linked above.
Reviews
No reviews yet, be the first.
Write a review
Versions
- v0.1.0 Imported from the upstream source.